首页 > 解决方案 > WebRequest\HttpClient C# 中的客户端证书身份验证失败

问题描述

我们的客户在通过客户端证书身份验证进行身份验证时遇到了失败。

使用 IISCrypto 完成了他们的 Tls1.2 配置。SSL 协议和 TLS1.1 被标记为禁用。

并且他们使用 IISCrypto 完成了他们的 Cipher Suite 配置。

我检查了 Cipher Suite\.net Framework 版本\操作系统版本\TLS 重新协商\服务器证书设置\API 网关设置\Windows 服务。但我无法得到任何结果。

然后我尝试编写一些故障排除程序来测试通信和握手顺序。我什么都没有。

Curl.exe并且openssl s_client在使用客户端证书发送消息时没问题,但在 C# 中总是失败,我的代码如下:

X509Certificate2 cert = new X509Certificate2(@"C:\Communicate.pfx", "xxxxx");
HttpClient client = null;
System.Net.Http.WebRequestHandler _internalHandler = new System.Net.Http.WebRequestHandler();
_internalHandler.UseProxy = false;
_internalHandler.ClientCertificates.Add(cert);
_internalHandler.ServerCertificateValidationCallback = ((obj, x509, chain, policyError) =>
{
     return true;
});
client = new HttpClient(_internalHandler);
client.BaseAddress = new Uri(string.Format("https://{0}:{1}/", args[0], int.Parse(args[1])));

=================================更新================= ===== 我试着写了一个TCP requester来测试。它成功了。似乎客户端证书选择处理程序在 和 之间是不同HttpClient\WebRequestSslStream

X509Certificate2 cert = new X509Certificate2(@"C:\Communicate.pfx", "xxxxxxxx");
            X509Certificate2Collection col = new X509Certificate2Collection();
            col.Add(cert);
            TcpClient client = new TcpClient(args[0], int.Parse(args[1]));
            Stream stream = client.GetStream();
            SslStream ssl = new SslStream(stream, false, new RemoteCertificateValidationCallback((a, b, c, d) =>
            {
                return true;
            }), 
            new LocalCertificateSelectionCallback((sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers)=>
            {
                return cert;
            }));
            ssl.AuthenticateAsClient("1.1.1.1", col, System.Security.Authentication.SslProtocols.Tls12, false);
            
            string x = "GET /api/TimebasedProxy/ HTTP/1.1\r\n";
            x += "Host:1.1.1.1\r\n";
            x += "Content-Type: application/json\r\n";
            x += "Connection: Close\r\n\r\n";
            byte[] xs = Encoding.UTF8.GetBytes(x);
            ssl.Write(xs, 0, xs.Length);

================更新==============

我得到了原因: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\SendTrustedIssuerList在服务器端设置为 1。(https://docs.microsoft.com/en-us/windows-server/security/tls/tls-registry-settings#sendtrustedissuerlist

但是客户在 Windows Server 2012R2 下运行他们的服务器,谁设置SendTrustedIssuerList为 1?==========================更新============= IISCrypto做到了。你对 IISCrypto 做了什么,IISCrypto 总是设置SendTrustedIssuerList为 1。这是一个错误。

标签: c#ssltls1.2client-certificates

解决方案


我得到了原因: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\SendTrustedIssuerList在服务器端设置为 1。(https://docs.microsoft.com/en-us/windows-server/security/tls/tls-registry-settings#sendtrustedissuerlist

但是客户在 Windows Server 2012R2 下运行他们的服务器,谁设置SendTrustedIssuerList为 1?

IISCrypto 做到了。你对 IISCrypto 做了什么,IISCrypto 总是设置SendTrustedIssuerList为 1。这是一个错误。


推荐阅读