Connection to the server has been successfully established, but an error occurred during login. (Provider: SSL Provider, Error: 0 – The certificate chain was issued by an untrusted issuer.)”

Time:2023-11-16

        Today when I was writing a .NET6 API, I used the Dapper framework to configure the database connection, and after configuring it, I got an error when linking the DB, and found the error to beMicrosoft.Data.SqlClientcaused by the use ofSystem.Data.SqlClient Then it can be accessed normally with the following error message:

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 – The certificate chain is issued by an untrusted authority.) “

System.Data.SqlClient is the .NET Framework’s old provider using ADO.NET.
Microsoft.Data.SqlClient package was released in 2019 and it is the new package that supports both .NET Core and .

Data.SqlClient program package in the link database, encryption defaults to True, so will verify the server TLS/SSL certificate, resulting in errors:Certificate chains are issued by untrusted issuers.

Program I:

The solution is actually quite simple, just add the DB link string followed by theEncrypt=false;string to solve the problem.

// Example Add encrypt=false; at the end of the DB link string.
"ConnectionStrings": {
    "Default": "Data Source=.;Initial Catalog=AuoUserdata;User Id=sa;Password=sa123;Trusted_Connection=True;Encrypt=false;"
  }

Official Microsoft Documentation Description:

The default value of the connection setting has been false changed from to true . With the growing use of cloud databases and the need to ensure those connections are secure, it’s time for this backwards-compatibility-breaking change.

The default value for connection settings has been changed from false to true . With the increasing use of cloud databases and the need to secure these connections, it was time for this backward compatibility breaking change.

Portal:What’s New in Microsoft.Data.SqlClient 4.0 – Encryption Default Set to True


Program II:

on account ofMicrosoft.Data.SqlClient Linked databases validate server TLS/SSL certificates by default, so we just need to add Auto Trust Server Security to skip validating the certificates.

// Example Add trustServerCertificate=true; at the end of the DB link string.
"ConnectionStrings": {
    "Default": "Data Source=.;Initial Catalog=AuoUserdata;User Id=sa;Password=sa123;trustServerCertificate=true;"
  }

trustServerCertificate

True if the server Transport Layer Security (TLS) (formerly known as Secure Sockets Layer (SSL)) certificate should be automatically trusted when using the TLS encryption communication layer. Otherwisefalse

note

in the event thattrustServerCertificate property is set to true, the SQL Server TLS/SSL certificate is automatically trusted when using the TLS encryption communication layer. In other words, the Microsoft JDBC Driver for SQL Server will not validate SQL Server TLS/SSL certificates. The default value isfalse

If thetrustServerCertificate property is set to false, the Microsoft JDBC Driver for SQL Server verifies the server TLS/SSL certificate.

Portal:SetTrustServerCertificate method

Conclusion:

The above two options can be chosen at random to solve the problem, if not, welcome to correct. It is not easy to organize, please give apointing and calling (e.g. camera)orfocusThanks!

Reference:

Error occurs when using Dapper Certificate chain is issued by untrusted issuing authority – jianshu.com

Connection to the server has been successfully established, but then an error occurs during login Error from .Net Core WebAPI

Reprinted with attribution:https://blog.csdn.net/Csongxuan/article/details/130335700

Recommended Today

Resolved the Java. SQL. SQLNonTransientConnectionException: Could not create connection to the database server abnormal correctly solved

Resolved Java. SQL. SQLNonTransientConnectionException: Could not create connection to the database server abnormal correct solution, kiss measuring effective!!!!!! Article Catalog report an error problemSolutionscureexchanges report an error problem java.sql.SQLNonTransientConnectionException:Could not create connection to database server Solutions The error “java.sql.SQLNonTransientConnectionException:Could not create connection to database server” is usually caused by an inability to connect to the […]