C# 代码:
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import("C:\\SomePath\\MyCertificate.pfx", "MyPassword", X509KeyStorageFlags.PersistKeySet);
var cert = collection[0];
ServicePointManager.SecurityProtocol = ...;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true;
handler.ClientCertificates.Add(cert);
var content = new ByteArrayContent(Encoding.GetEncoding("latin1").GetBytes("Hello world"));
HttpClient client = new HttpClient(handler);
var resp = client.PostAsync(requestUri: url, content: content).Result.Content.ReadAsStringAsync().Result;
适用于:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
出现错误:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
.Net错误信息:SocketException: 远程主机强制关闭了一个现有的连接
.Net版本:4.7.1
操作系统:Windows 10版本1703(支持的密码列表:https://msdn.microsoft.com/en-us/library/windows/desktop/mt808163(v=vs.85).aspx) - 服务器指定使用TLS_RSA_WITH_AES_256_GCM_SHA384,这是受支持的密码之一。
在wireshark中,我可以看到在工作的调用中(C# / Tls 1.1和Curl Tls 1.2),证书被发送到服务器。这是C# tls 1.1调用的wireshark转储:
然而,在wireshark中,我也可以看到使用C# / Tls 1.2时,客户端未向服务器发送证书。这是C# tls 1.2调用的wireshark转储:
有人能看出我错过了什么吗?
更新
看起来证书具有不受Windows中的Schannel支持的md5签名,与tls 1.2结合使用。我们的供应商为我们创建了另一个证书作为解决方案。
我遇到了这个随机的线程,讨论了这个问题:https://community.qualys.com/thread/15498