zno*_*tny 6 c# ssl kestrel-http-server asp.net-core
我试图通过其appsettings.json配置文件在 Linux 环境中的基于 ASP.NET Core MVC 的服务应用程序中设置 Kestrel 服务器,以便它恰好有两个端点,都使用 HTTPS,但每个端点都具有不同的证书。一个端点监听0.0.0.0,另一个只监听localhost。该服务充当 IdentityServer4 的主机。服务器的预期运行时环境将是一个 Docker 容器,它是基于多服务的系统的一部分,目前作为 docker-compose 集群运行。
我已经研究了https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2#endpoint-configuration 上的官方文档,并认为它应该很容易实现。我创建了一个自签名根 CA 证书并生成了两个由根签名的证书。其中之一将 Common Name 设置为容器在集群中的名称,用于与外部环境的安全通信。另一个证书将 CN 设置为localhost并使用,因为服务器有多个可从外部使用的控制器,它们都将转换传入请求并转发到将处理它们的特殊内部控制器。(我意识到这对我来说有点偏执,但这无关紧要。)
我尝试配置 Kestrel,以便主端点使用默认证书,localhost端点使用临时证书,即像这样:
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://+:2051"
},
"HttpsLocalhost": {
"Url": "https://localhost:2052",
"Certificate:": {
"Path": "path/to/localhost-dev.pfx",
"Password": "password"
}
}
},
"Certificates": {
"Default": {
"Path": "path/to/identity-api-dev.pfx",
"Password": "password"
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据文档,这似乎是正确的配置,其中说如果明确配置 HTTPS,服务器需要定义默认证书。
外向控制器使用延迟初始化HttpClient,其BaseAddress属性在第一个接收到的请求上初始化,并带有localhost端点的 URL 。使用此方法可以轻松确定该 URL
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://+:2051"
},
"HttpsLocalhost": {
"Url": "https://localhost:2052",
"Certificate:": {
"Path": "path/to/localhost-dev.pfx",
"Password": "password"
}
}
},
"Certificates": {
"Default": {
"Path": "path/to/identity-api-dev.pfx",
"Password": "password"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后将上述方法返回的地址传递给以下方法,该方法用它初始化控制器的HttpClient实例,并查询 IS4 的 OpenID 配置端点以获取发现文档,并使用它进一步初始化控制器:
private string GetIdentityServiceHostAndPort()
{
IServerAddressesFeature addresses = Server.Features.Get<IServerAddressesFeature>();
// Get the first that uses HTTPS and is on localhost
foreach (string url in addresses.Addresses)
{
if (url.StartsWith("https://localhost"))
{
return url;
}
}
throw new ArgumentException("Could not determine identity service's host/port, is it listening on localhost using HTTPS?");
}
Run Code Online (Sandbox Code Playgroud)
由于我已经花费了大量时间来尝试调试此问题,因此我已确保将服务器配置为使用一个端口侦听任何地址并侦听另一个端口localhost。该GetIdentityServiceHostAndPort()方法确实返回了正确的 URL。
现在,我面临的问题是上述方法中对发现文档的请求以
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
Run Code Online (Sandbox Code Playgroud)
显然,证书验证过程失败了,这对我来说似乎很可疑,因为请求应该发送到具有CN=localhostset证书的端点。
当我尝试手动请求发现文档时
curl -g 'https://localhost:2052/.well-known/openid-configuration'
Run Code Online (Sandbox Code Playgroud)
我收到了这条消息
curl: (60) SSL: certificate subject name 'identity.api' does not match target host name 'localhost'
Run Code Online (Sandbox Code Playgroud)
我不明白为什么服务器会尝试发送此证书。我知道这是配置的默认值,但也有为localhost端点明确定义的证书,我希望在这种情况下使用该证书。
我确定我遗漏了一些东西,但正如我所说,我已经花了几个小时试图找到罪魁祸首,所以我将非常感谢您提供的任何意见。
只是为了澄清这一点,同时让自己在整个开发者世界面前感到尴尬,问题是我经常忽略 Kestrel 配置部分中的拼写错误。Certificate该小节的关键字符串中有一个无关的冒号HttpsLocalhost,如果我的生活依赖于它,我就无法发现它。
值得庆幸的是,它没有,当然,此修复后配置有效。
| 归档时间: |
|
| 查看次数: |
1488 次 |
| 最近记录: |