Liunx 服务器出现了服务器证书失效,部署的访问链接无法启动
Unhandled exception. System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date. 这个错误如何处理
这个错误是因为 ASP.NET Core 应用尝试使用 HTTPS,但找不到有效的开发证书。也就是服务器的https服务,找不到有效的证书,所以要通过以下方式,对服务器的IIS证书进行刷新。
以下是详细的解决方法:
解决方案
1. 安装/修复开发证书
运行以下命令(适用于 Windows/macOS/Linux):
dotnet dev-certs https --trust
-
在 Windows 上,这会自动将证书安装到当前用户的信任存储中。
-
在 macOS 上,可能需要手动信任证书(打开 Keychain Access,找到
localhost证书并设置为始终信任)。 -
在 Linux 上,可能需要额外配置(如手动信任证书)。
2. 清除并重新安装证书(如果上述无效)
dotnet dev-certs https --clean
dotnet dev-certs https --trust
然后重启 IDE 或终端。
3. 检查证书是否存在
运行以下命令查看证书是否存在:
dotnet dev-certs https --check
返回:[root@iZdz56exlbxbsdZ ~]# dotnet dev-certs https --check
A valid certificate was found: F160D99E0CB4273D26CCD370585880570E3DD8ED - CN=localhost - Valid from 2025-07-17 13:53:19Z to 2026-07-17 13:53:19Z - IsHttpsDevelopmentCertificate: true - IsExportable: true
则通过证书,并提示证书有效期为2026年7月17日止。
如果返回 A valid certificate was found,说明证书已存在,可能是权限问题。
4. 手动指定证书(可选)
如果仍然有问题,可以在 Program.cs 中手动指定证书路径:
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.ServerCertificate = new X509Certificate2("path/to/cert.pfx", "password");
});
});
5. 临时禁用 HTTPS(仅限开发环境)
如果只是本地开发,可以在 launchSettings.json 中移除 HTTPS 配置:
{
"profiles": {
"YourApp": {
"commandName": "Project",
"applicationUrl": "http://localhost:5000", // 只保留 HTTP
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
6. 其他可能的原因
-
权限问题:以管理员身份运行终端或 Visual Studio。
-
证书过期:运行
dotnet dev-certs https --check确认证书是否有效。 -
Docker 环境:如果使用 Docker,需确保容器内信任证书(或禁用 HTTPS)。
验证是否解决
重启应用后,访问 https://localhost:5001(或你的 HTTPS 端口),确认不再报错。
附:各系统下的注意事项
| 系统 | 关键步骤 |
|---|---|
| Windows | 以管理员运行命令提示符或 PowerShell。 |
| macOS | 手动在 Keychain Access 中信任 localhost 证书。 |
| Linux | 可能需要运行 sudo update-ca-certificates 更新证书存储。 |
浙公网安备 33010602011771号