You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 9 Web API连接成功后登录报错如何解决?

问题描述

我有一个基于ASP.NET Core 9的Web API项目,搭配SQL Server 2019,连接字符串定义在appsettings.json中如下:

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

同时,在Program.cs中配置DbContext如下:

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

执行POST操作调用_context.SaveChangesAsync();时,抛出以下错误:

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.

解决方案

该错误源于SQL Server的SSL证书未被客户端信任,可通过以下方式解决:

方法1:修改连接字符串添加信任参数

在连接字符串末尾追加TrustServerCertificate=true,修改后内容如下:

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

此方式会跳过证书链验证,直接信任SQL Server提供的证书,适合开发环境快速解决问题。

方法2:将SQL Server证书导入受信任根证书库

  1. 打开SQL Server配置管理器,依次展开SQL Server网络配置 -> 目标实例的协议,右键选择属性
  2. 在证书选项卡中,查看当前SQL Server使用的证书,记录证书指纹或导出证书文件
  3. 打开Windows管理控制台(mmc),添加证书管理单元,选择计算机账户
  4. 展开受信任的根证书颁发机构 -> 证书,右键选择所有任务 -> 导入
  5. 按照向导完成证书导入,客户端将信任该证书,适合需要保持SSL加密的场景

方法3:关闭SQL Server强制加密(不推荐生产环境)

若无需SSL加密连接,可关闭强制加密:

  1. 打开SQL Server配置管理器,找到目标实例的协议,右键选择属性
  2. 在标志选项卡中,将强制加密设置为否
  3. 重启SQL Server服务使设置生效

内容的提问来源于stack exchange,提问作者R. Nourmandi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:24:58