【发布时间】: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。
【问题讨论】: