c# - 具有不同 TLS 证书的多端点 Kestrel 配置提供错误的证书

标签 c# ssl asp.net-core kestrel-http-server

我正在尝试通过其 appsettings.json 配置文件在 Linux 环境中基于 ASP.NET Core MVC 的服务应用程序中设置 Kestrel 服务器,以便它具有两个端点,均使用HTTPS,但每个都有不同的证书。一个端点在 0.0.0.0 上监听,另一个端点仅在 localhost 上监听。该服务充当 IdentityServer4 的主机。服务器的预期运行时环境将是 Docker 容器,它是基于多服务的系统的一部分,当前作为 docker-compose 集群运行。

我研究了官方文档https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2#endpoint-configuration并假设它应该很容易实现。我创建了一个自签名根 CA 证书并生成了两个由根签名的证书。其中之一将通用名称设置为容器在集群中将具有的名称,用于与外部环境进行安全通信。另一个证书的 CN 设置为 localhost ,之所以使用它,是因为服务器有多个可从外部使用的 Controller ,它们都将转换传入的请求并转发到将处理它们的特殊内部 Controller 。 (我意识到我有点偏执,但这不是重点。)

我尝试配置 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,服务器需要定义默认证书。

向外的 Controller 使用延迟初始化的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?");
}

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

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 端点明确定义的证书,我希望在这种情况下使用该证书。

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

最佳答案

为了澄清这一点,同时让自己在整个开发者世界面前感到尴尬,问题是我经常忽略 Kestrel 配置部分中的拼写错误。 HttpsLocalhost 小节中的 Certificate 键字符串中有一个无关的冒号,如果我的生活依赖于它,我就无法发现它。

幸运的是,它没有,当然,此修复后配置有效。

关于c# - 具有不同 TLS 证书的多端点 Kestrel 配置提供错误的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57535741/

相关文章:

c# - DbDataReader、NextResult() 和填充多个表

ssl - 您能否终止 SSL 以提供自定义错误页面,但在传递到目标服务器之前重新加密?

php - Symfony 1.4 通过 SSL 连接到 mysql

c# - 如何在RequireAssersion中访问HttpContext和Request?

AngularJS:UI-Router 解决路由 hash-bang 不起作用

c# - 如何在 Blazor 中创建通用 TreeView 组件?

c# - CA2101 : Specify marshaling for P-Invoke string arguments

c# - 使用 autofixture 创建结构不会引发公共(public)构造函数错误

objective-c - iOS 5 : https ( ASIHTTPRequest) stop working

c# - 声明一个常量数组