【问题标题】:How to start a trusted TLS connection with a server by IP address using HttpClient in C#?如何使用 C# 中的 HttpClient 通过 IP 地址启动与服务器的受信任 TLS 连接?
【发布时间】:2018-08-13 06:58:02
【问题描述】:

我正在尝试在 Windows 上使用 C# 的 HttpClient 与在(本地)VM 中运行的应用程序建立 TLS 连接。但是,它每次都会导致RemoteCertificateNameMismatch 错误。

这段代码:

HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (request, cert, chain, policyErrors) =>
{
    Console.WriteLine($"Policy Errors: {policyErrors}");
    return policyErrors == SslPolicyErrors.None;
};
HttpClient httpClient = new HttpClient(handler)
{
    BaseAddress = new Uri("https://192.168.99.100/engine-rest")
};
var result = httpClient.GetAsync("/engine").Result;

导致此错误:

Policy Errors: RemoteCertificateNameMismatch

Unhandled Exception: System.AggregateException: One or more errors occurred. (An error occurred while sending the request.) ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpxception: A security error occurred
   at System.Net.Http.WinHttpRequestCallback.OnRequestSendingRequest(WinHttpRequestState state)
   at System.Net.Http.WinHttpRequestCallback.RequestCallback(IntPtr handle, WinHttpRequestState state, UInt32 internetStatus, IntPtr statusInformation, UInt32 statusInformationLength)

但是,我的证书在 Google Chrome 和 Mozilla Firefox 中有效,但对 Internet Explorer 无效。

查看从 Chrome 截取的屏幕截图。证书似乎有效。我已经创建了自己的证书颁发机构证书(自签名证书),然后我用它来创建我的服务器证书。我填写了主题名称。作为主题备用名称 (SAN),我添加了 IP 地址。

在 IE 和 HttpClient 中不支持 SAN 字段中的 IP 地址字段吗?如果是这样,是否有其他方法可以使用 IP 地址验证服务器证书?

我正在使用 dotnet core 2.0。

【问题讨论】:

    标签: c# .net ssl .net-core


    【解决方案1】:

    阅读了一个类似的问题,我发现了以下内容:

    .NET 不直接支持将 IP 地址作为主题备用名称,因为它依赖 Schannel 对证书中的 IP 地址进行验证。

    Schannel 仅在证书的“DNS 名称 =”字段中查找 https 请求中给出的主机名 (IP)。因此,如果您可以让 CA 为您提供“DNS 名称 =”字段中列出的 IP 地址的证书,那么它可能会起作用。

    您可以尝试使用“DNS 名称”字段。

    来源:Does .NET web service client support SSL cert with IP in Alternate Subject Name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2010-09-13
      • 2014-07-31
      • 1970-01-01
      • 2011-10-04
      • 2020-07-10
      相关资源
      最近更新 更多