【问题标题】:Multi-endpoint Kestrel configuration with different TLS certificates serves the wrong certificate具有不同 TLS 证书的多端点 Kestrel 配置提供错误的证书
【发布时间】:2019-08-17 11:10:11
【问题描述】:

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

由于我已经花费了大量时间来尝试调试此问题,因此我已确保服务器配置为使用一个端口侦听任何地址,以及使用另一个端口侦听 localhostGetIdentityServiceHostAndPort() 方法确实返回了正确的 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# ssl asp.net-core kestrel-http-server


    【解决方案1】:

    只是为了说明这一点,同时在整个开发人员世界面前让自己感到尴尬,问题是我经常忽略 Kestrel 配置部分中的一个错字。 HttpsLocalhost 小节中的 Certificate 键字符串中有一个多余的冒号,如果我的生活依赖它,我无法发现。

    谢天谢地,它没有,当然配置在此修复后工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多