SqlClient.SqlException - The certificate chain was issued by an authority that is not trusted
I got the following error the other day when connecting to a local SQL server database from ASP.NET Core:
Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904):
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.)
...
Cause of the SSL error
The reason for the error is that SQL Server uses a self-signed SSL certificate to encrypt the connection to the database which is not trusted by default by the ASP.NET Core client.
This is the connection string that was getting the error:
"Server=localhost; Database=my-database; User Id=test; Password=pass123;"
How to fix the SSL error
To fix the error I configured ASP.NET Core to trust the server certificate by adding TrustServerCertificate=true
to the connection string.
This is the connection string that fixed the SSL error:
"Server=localhost; Database=my-database; User Id=test; Password=pass123; TrustServerCertificate=true;"
Subscribe or Follow Me For Updates
Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.
- Follow me on Twitter at https://twitter.com/jason_watmore
- Subscribe on YouTube at https://www.youtube.com/JasonWatmore
- Follow me on Facebook at https://www.facebook.com/JasonWatmoreBlog
- Follow me on GitHub at https://github.com/cornflourblue
- Feed formats available: RSS, Atom, JSON
Other than coding...
I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.
- Subscribe on YouTube at https://www.youtube.com/TinaAndJason
- Follow us on Instagram at https://www.instagram.com/tinaandjason
- Follow us on Facebook at https://www.facebook.com/TinaAndJasonVlog
- Visit our website at https://tinaandjason.com.au
Need Some .NET Help?
Search fiverr to find help quickly from experienced .NET developers.