首页 > 解决方案 > 具有不同 TLS 证书的多端点 Kestrel 配置提供错误的证书

问题描述

我正在尝试通过其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"
      }
    }
  }

根据文档,这似乎是正确的配置,如果显式配置 HTTPS,则服务器需要定义默认证书。

面向外的控制器使用延迟初始化HttpClient,其BaseAddress属性在第一个接收到的带有localhost端点 URL 的请求上初始化。使用此方法可以轻松确定该 URL

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?");
}

然后将上述方法返回的地址传递给以下方法,该方法HttpClient用它初始化控制器的实例,并查询 IS4 的 OpenID 配置端点以获取发现文档并使用它来进一步初始化控制器:

private static async Task InitializeTokenEndpointHttpClientAsync(string baseAddress)
{
    TokenEndpointClient = new HttpClient();
    TokenEndpointClient.BaseAddress = new Uri(baseAddress);

    HttpResponseMessage response = await TokenEndpointClient.GetAsync(".well-known/openid-configuration");
    string content = await response.Content.ReadAsStringAsync();
    if (response.StatusCode != HttpStatusCode.OK)
    {
        throw new InvalidOperationException("Error querying IdentityServer for discovery document: " + content);
    }

    DiscoveryDocument document = JsonConvert.DeserializeObject<DiscoveryDocument>(content);
    Uri fullTokenEndpointUri = new Uri(document.token_endpoint);
    TokenEndpointPath = fullTokenEndpointUri.LocalPath;
}

由于我已经花费了大量时间来尝试调试此问题,因此我已确保将服务器配置为侦听具有一个端口的任何地址和另一个端口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.

显然,证书验证过程失败了,这对我来说似乎很可疑,因为该请求应该发送到确实具有已CN=localhost设置证书的端点。

当我尝试手动请求发现文档时

curl -g 'https://localhost:2052/.well-known/openid-configuration'

我收到了这条消息

curl: (60) SSL: certificate subject name 'identity.api' does not match target host name 'localhost'

我不明白为什么服务器会尝试发送此证书。我知道这是配置的默认值,但也有为localhost端点明确定义的证书,我希望在这种情况下使用该证书。

我确信我遗漏了一些东西,但正如我所说,我已经花了几个小时试图找到罪魁祸首,所以我将非常感谢任何输入。

标签: c#sslasp.net-corekestrel-http-server

解决方案


只是为了让自己在整个开发者世界面前变得清晰和尴尬,问题是我经常忽略 Kestrel 配置部分中的一个错字。Certificate该小节的密钥字符串中有一个多余的冒号,HttpsLocalhost如果我的生活依赖它,我无法发现它。

值得庆幸的是,它没有,当然配置在此修复后工作。


推荐阅读