Sudden - 'The certificate chain was issued by an authority that is not trusted in Microsoft.Data.SqlClient' in working project

Viewed 10006

I have an ASP.Net Webforms website running in IIS on a Windows Server. Also on this server is the SQL server.

Everything has been working fine with the site but now I am seeing issues with using a DataAdapter to fill a table.

So here is some code, please note it's just basic outline of code as actual code contains confidential information.

public List<Summary> Fetch(string Connection, int parameter1, int parameter2, bool parameter3)
{
     List<Summary> collection = new List<Summary>();

     using (SqlConnection dbConnection = new SqlConnection(Connection))
     {
        using (SqlCommand dbCommand = dbConnection.CreateCommand()) // Result is complex
        {
            dbCommand.CommandType = CommandType.StoredProcedure;
             
            //
            // Code to add parameters and set commandText goes here
            //

            using (SqlDataAdapter da = new SqlDataAdapter(dbCommand))
            {
                DataTable table = new DataTable();
                DataRow row;

                try
                {
                  da.Fill(table);
                }
                catch(Exception ex)
                {
                   // Log error
                }
                
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    row = table.Rows[i];
                    Summary data = Populate(row);
                    collection.Add(data);
                }
            }
         }
     }
     return collection;
}

The above is in a library and is called like this in the Web Forms site.

var Summaries = MyLibrary.Fetch(ConnectionString, 1, 111, false);

So as I say, everything was working fine. Now, all of a sudden the above has stopped working and Summaries is always empty.

To investigate I tried the following.

Created a test using xUnit with the same parameters used by the website. These were captured during debugging to ensure they matched. The result returned 1 item.

I then ran the stored procedure in SQL management Studio and it matched the xUnit test with 1 item returned.

I then checked SQL Profiler and this is where things seemed a little odd. Nothing was being recorded in the trace when the web-forms was calling the library.

So both the web-site and xUnit test were using the same library, passing the same parameters and yet one worked and the other didn't... very odd.

As a last resort, I added the library project to the Web Forms project and proceeded to debug through that... then I found the error.

da.Fill(table);

The above line generated the following exception.

Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll 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.)

Having a look around here on stack, I saw many responses with I need to install a certificate.

My question is, if this is the case then why and why now? Nothing has changed in the code from when it was working till now. The web-site and SQL server are on the same windows server. Also, why does the xunit test work without it and the website does not when I am debugging on the same machine!

The only items that I now has changed is install of .Net 6 and patch Tuesday resulted in some updates and a restart of the server.

4 Answers

Microsoft.Data.SqlClient 4.0 is using ENCRYPT=True by default. Either you put a certificate on the server (not a self signed one) or you put

TrustServerCertificate=Yes;

on the connection string.

I hit this issue as well. Net 6.0

It suddenly started occurring when I upgraded Microsoft.EntityFrameworkCore.SqlServer

FROM Version="6.0.0" to Version="6.0.1"

I added Encrypt and TrustServerCertificate to connection string.

data source=(local);Initial Catalog=XXXXXXX;Trusted_Connection=True;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True

I suddenly got the same problem when first upgrading Microsoft.Data.SqlClient to 4.x.x. I reverted back to 2.0.1 and the same problem was present.

I then removed all "bin" and "obj" folders for each project. And then executed a "dotnet restore". After that it worked as before.

Similar to others, we got this error after upgrading Microsoft.EntityFrameworkCore.SqlServer from 2.2.6 to 6.0.5 and Microsoft.Data.SqlClient from 3.0.0 to 4.1.0.

However, in our case removing Microsoft.Data.SqlClient fixed the issue.

I noticed that Microsoft.EntityFrameworkCore.SqlServer listed a dependency of Microsoft.Data.SqlClient 2.1.4 in the solution explorer, maybe that is why removing the other one worked.

Related