A connection was successfully established with the server, but then an error occurred during the login process Error from .Net core WebAPI

Amit Potdar 131 Reputation points
2021-12-14T12:23:52.077+00:00

Hi,

I've an application that uses WebAPI in .Net core 6 and uses Kestrel server. When calling HttpPost request, the code tries to inject data in DB but fails with error:

The certificate chain was issued by an authority that is not trusted.

Inner exception: A connection was successfully established with the server, but then an error occurred during the login process

The obvious resolution is to inject TrustServerCertificate=True in the connection string. But this is a security risk as it can trust invalid certificate.

I'm using Dapper package to communicate with Sql server database.

After investigation, I found that this happens when I refer Microsoft.Data.SqlClient package. If I replace with System.Data.SqlClient package, above error occurs no more and I don't have to inject TrustServerCertificate flag.

1) Can anyone let me know why this error is raised when I refer Microsoft.Data.SqlClient?
2) Which package (System.Data.SqlClient or Microsoft.Data.SqlClient) is recommended for the new development (i.e. .Net 6 and Kestrel)

Thanks,
Amit

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,167 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,718 questions
{count} votes

10 answers

Sort by: Newest
  1. Bala Murugan Velusamy 1 Reputation point
    2023-12-19T09:50:44.0166667+00:00

    This Connection string works well. you check your connection string similar to following example

    Example: 1
    Server=YourSystemName\SQLEXPRESS;Database=YourDb;Integrated Security=True;Trusted_Connection=True;TrustServerCertificate=True

    Example: 2
    Server=YourSqlInstance;Database=YourDb;Integrated Security=True;Trusted_Connection=True;TrustServerCertificate=True

    Example: 3 (if in appsettings.json use double slash "\")
    Server=YourSystemName\SQLEXPRESS;Database=YourDb;Integrated Security=True;Trusted_Connection=True;TrustServerCertificate=True

    0 comments No comments

  2. John Sonron 0 Reputation points
    2023-08-29T12:32:31.01+00:00

    I am facing a similar situation on my Business IT Solutions website server. If the Web API is accessed from a different domain, ensure that CORS settings are configured to allow requests from the client application's domain.

    0 comments No comments

  3. Aaron Obed Giron Garrido 0 Reputation points
    2023-07-05T20:32:57.5733333+00:00

    Hello

    I have a problem when I try to generate the dbContext classes in ASP .NET Core Web API, I try many solutions, but I have not succeeded yet, does anyone know any guide to help me solve it.


  4. Rijwan Ansari 746 Reputation points MVP
    2023-01-11T13:12:45.9433333+00:00

    Hi @Amit Potdar

    I faced similar issues a few months back.

    Here the solution [https://rijsat.com/2022/12/12/entity-framework-update-database-error-the-certificate-chain-was-issued-by-an-authority-that-is-not-trusted/

    Update Your connection String as shown below.

    
    
    

    We need to add additional parameters trusted_connection=true;encrypt=false; in the connection string.

    This is because of SQL server update.

    The default value of the Encrypt connection setting has been changed from false 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.

    4 people found this answer helpful.

  5. Darren Lawrence 6 Reputation points
    2022-11-09T17:04:53.937+00:00

    Had this issue today

    Excel connection to SQL Data source, failed with this exact same message.

    Turns out we only needed to install the SQL Client Tools v11 x64 and re-open the workbook and all worked again.

    1 person found this answer helpful.