Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "The target principal name is incorrect error" with Microsoft.Data.SqlClient Version=4.0.0

After upgrading to Microsoft.Data.SqlClient Version=4.0.0 got the error:

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The target principal name is incorrect.

I looked up the breaking change https://github.com/dotnet/SqlClient/pull/1210 and then set Encrypt=False in the connection string, however the error remained the same.

The error went away only by reverting to 2.0.1

Could anyone please suggest how to fix the issue in 4.0.0 ?

like image 624
Anand Avatar asked Dec 19 '25 12:12

Anand


1 Answers

AlwaysLearning posted the answer in the comments:

The target principal name is incorrect signifies that the Name or IP Address you're using in the Server=...; portion of your connection string does not match a CommonName or SubjectAltName in the X.509 certificate provided by the SQL Server which isn't surprising because, by default, SQL Server installs with a self-signed certificate using only the current host name. Does the error go away if you include TrustServerCertificate=False; in (all of) your connection strings?

Argh, replace TrustServerCertificate=False; with TrustServerCertificate=True; ... it's meant to trust whatever certificate is presented regardless of the CommonName, SubjectAltName(s) and certificate chain.

So the answer was

TrustServerCertificate=True

*Adding this to accurately show that the question has been answered. Community Wikis do not generate points, so this is not an attempt to steal points. AlwaysLearning should post the answer to get credit.

like image 99
ElRojo Avatar answered Dec 21 '25 07:12

ElRojo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!