我目前正在 Linux 虚拟机(我正在 PC 上工作)上将 C#/.NET 应用程序从 .NET 6.0 升级到 .NET 8.0,并且在使 HTTPS 端点正常工作时遇到一些问题。我在寻找解决方案时偶然发现了这篇文章,但我不能 100% 确定这确实是问题所在,如果是的话,我不知道如何解决它:)。我正在从本地文件系统检索
X509Certificate2
crt/密钥对,并使用它来创建使用 HTTPS 的 IHostBuilder
对象。以下是我正在使用的当前代码:
var certContents = File.ReadAllText(<local filepath>);
var certKeyContents = File.ReadAllText(<local key filepath>);
var cert = X509Certificate2.CreateFromPem(certContents, certKeyContents);
return Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Any, port, ops =>
{
ops.UseHttps(cert);
});
}
})
.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
})
.UseNLog();
这样做的结果通常是一个错误页面,仅显示“此站点无法提供安全连接”。我主要使用 Google Chrome v122.0 和 Postman v9.31 来测试这些调用。我在代码中设置了断点,似乎我什至无法通过这些错误到达后端,因为这些断点永远不会被命中。有趣的是,我确实发现当我使用 FireFox 时我可以走得更远(例如,它似乎能够访问后端);它不会抛出 SSL 错误,而是尝试访问我们应用程序的 Keycloak 实例(非本地托管)以验证我们的 SSO 令牌并抛出 IDX20803 错误。下面是进一步的异常代码:
System.InvalidOperationException: IDX20803: Unable to obtain configuration from: '<Keycloak URL>'.
at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
我还注意到一些 IDX20803 错误更多地遵循 this 模式。 这段代码在 .NET 6 中运行得很好,但我一定在更新的 .NET 版本中遗漏了一些东西 - 任何指导都值得赞赏!
我已经尝试过的
options.Listen()
回调中,尝试添加以下代码块。
opt.AllowAnyClientCertificate();
opt.ClientCertificateValidation = (certif, chain, policyErrors) => true;
opt.ClientCertificateMode = Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.AllowCertificate;
opt.ServerCertificate = cert;
opt.SslProtocols = System.Security.Authentication.SslProtocols.Tls12
| System.Security.Authentication.SslProtocols.Ssl3
| System.Security.Authentication.SslProtocols.Tls
| System.Security.Authentication.SslProtocols.None
| System.Security.Authentication.SslProtocols.Tls11
| System.Security.Authentication.SslProtocols.Tls13
| System.Security.Authentication.SslProtocols.Default;
在
options.Listen()
回调中,尝试删除 ops.UseHttps(cert)
调用
在我的 Program.cs 和 Startup.cs 文件的开头,尝试添加此行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.SystemDefault | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Tls13;
在Startup.cs文件的Configure方法中添加
app.UseHttpsRedirection();
可能与此相关:https://github.com/dotnet/aspnetcore/issues/54366
看看这是否有帮助:
options =>
{
options.Listen(System.Net.IPAddress.Loopback, your-port, httpnOptions =>
{
httpOptions.UseHttps(new HttpsConnectionAdapterOptions { SslProtocols = SslProtocols.Tls12 });
}
}))
如果有帮助,则说明 TLS 1.3 存在问题。看起来他们可能会在五月份发布补丁。