migrationado.net.net-8.0sqlconnection.net-4.7

SqlConnection.Open works for System.Data.SqlClient and fails for Microsoft.Data.SqlClient with same connection string


I upgraded a library from .NET 4.7 to .NET 8.0.

In doing so, I had to update my System.Data.SqlClient references to Microsoft.Data.SqlClient.

I have both projects open, in different VS sessions, side by side.

Both are passing the exact same connection string to the Open method on their respective versions of SqlConnection:

Data Source=MyDbServer;Initial Catalog=MyDatabase;Application Name=MyApplication;Pooling='true';Connection Lifetime=500;Integrated Security=SSPI;Persist Security Info=True;

The System.Data.SqlClient version of SqlConnection opens without trouble.

The Microsoft.Data.SqlClient version of SqlConnection raises this exception on Open():

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 was issued by an authority that is not trusted.)'

Two questions:

  1. Why does the System.* version succeed, when the Microsoft.* version doesn't?

  2. What do I need to change about my connection string to make this work? (i.e. - I don't want to change anything about the db server or db itself to get this to work... I only want to change the connection string)

Thanks


Solution

  • For Question 1,

    Microsoft.Data.SqlClient enforces stricter security defaults compared to System.Data.SqlClient.

    System.Data.SqlClient silently skips some SSL/TLS validation scenarios while Microsoft.Data.SqlClient requires trusted certificates by default, and if your SQL Server is using a self-signed or internal certificate that isn’t trusted by your client machine, it throws exactly this error.

    For Question 2, Add TrustServerCertificate=true; in your connection string.