Search code examples
sql-serverasp.net-core-webapiasp.net-core-9.0

Why an error occurred after connection was established successfully in an ASP.NET Core 9 Web API?


I have an ASP.NET Core 9 Web API project with SQL Server 2019, and the connection string is defined in the appsettings.json as shown below:

"AllowedHosts": "*",
"ConnectionStrings": {
    "DefaultConnection": "Data Source = .; initial Catalog = serverAPI9; Trusted_Connection = true" }
}

Also, the connection is configured in the Program.cs like this:

builder.Services.AddDbContext<DataContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});

While performing a POST action, the _context.SaveChangesAsync(); method throws the following error:

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.)

System.ComponentModel.Win32Exception (0x80090325): The certificate chain was issued by an authority that is not trusted.

How to solve this problem?


Solution

  • As per my suggestion.

    You do not specified the login part in the connection string

    • Like if you have to connect sql server with the Windows Based Authentication then use Integrated Security=True;

      Server=server;Database=database-name;Connect Timeout=120;Encrypt=False;MultipleActiveResultSets=true;Integrated Security=True;

    • Used SQL Server Authentication then Use User ID=usename; Password=password;

      Server=server;Database=database-name;User ID=username;Password=password;Connect Timeout=120;Encrypt=False;TrustServerCertificate=False;MultipleActiveResultSets=true

    • Use TrustServerCertificate=true when you connect server using Window Based Authentication for the SQL Server Authentication we do not required

    Please find out related document from the official Microsoft site :Connection string syntax