C# .Net 的 TLS 1.2 错误:底层连接已关闭:发送时发生意外错误

Sav*_*dar 5 c# asp.net ssl webclient tls1.2

我有 ac# .net 站点,我的站点向银行请求支付系统。大约 3-4 天前一切都很好,但现在我无法从我的服务器请求银行服务器。我收到此错误:“基础连接已关闭:发送时发生意外错误。” 当我尝试向银行提出请求时。

当我从 .net c# 代码请求它时出现此错误。

"The underlying connection was closed: An unexpected error occurred on a send."
Run Code Online (Sandbox Code Playgroud)

这是我的代码;

    public string Send(string request)
{
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    try
    {
        string postData = "";
        string responseData = "";
        System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("ISO-8859-9");

        postData = "https://xxxxxxxxx.aspx?data=[DATA]";
        postData = postData.Replace("[DATA]", request);
        HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(postData);
        webReq.Timeout = 60000;
        webReq.KeepAlive = false;
        webReq.Method = "GET";
        WebResponse webResp = webReq.GetResponse();
        Stream respStream = webResp.GetResponseStream();

        byte[] buffer = new byte[10000];
        int len = 0, r = 1;
        while (r > 0)
        {
            r = respStream.Read(buffer, len, 10000 - len);
            len += r;
        }
        respStream.Close();
        responseData = encoding.GetString(buffer, 0, len).Replace("\r", "").Replace("\n", "");
        return responseData;
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        return null;
    }
    catch (Exception ex)
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我从 IE 11 尝试时,我收到此错误。

在此处输入图片说明 在此处输入图片说明

当我尝试 IE 11 时,EventViewer 显示错误。

A fatal alert was received from the remote endpoint. The TLS protocol defined fatal alert code is 40.
Run Code Online (Sandbox Code Playgroud)

但是这里有一个有趣的事情,Chrome 和 Firefox 可以去同一个地址。

在此处输入图片说明

  • 操作系统:Windows Server 2012 R2
    • 已安装所有证书。
    • 为 POODLE 配置的 TLS 设置。(来自:http ://wiki.maestropanel.com/windows-server-ssl-tls-yapilandirmasi/ )
    • IIS 版本:8.5
    • .Net 版本:4.5

感谢帮助!

Jon*_*der 2

银行网站可能更改了其 SSL 证书,或者以其他方式更改了其安全配置,这样当您的客户端发送它能够在其初始 SSL 握手/协商“Client Hello”消息中接受的 cipher_suites 值列表时,就会有与银行(现在)愿意支持的不匹配。

Chrome 和 Firefox(显然)有自己的 cipher_suites 值集,这些值独立于操作系统的配置值,这就是为什么它们仍然可以工作而 Internet Explorer 却不能工作的原因。

我建议下载Microsoft Message Analyzer,并使用它对当您尝试与银行网站(在 C# 应用程序或 Internet Explorer 中)建立 SSL 连接但失败时发生的 SSL 协商运行跟踪。然后,运行另一个跟踪,了解 SSL 协商成功时发生的情况(在 Firefox 或 Chrome 中)。

希望您会看到两个 Client Hello 消息之间的一些差异,从而使您能够查明失败的 SSL 协商是如何导致其失败的。然后您应该能够对 Windows 进行配置更改以使其成功。 IISCrypto是一个很好的工具(即使对于客户端 PC,尽管名称为“IIS”)。

以下两个 Windows 注册表项控制您的 PC 将使用的 cipher_suites 值:

  • HKLM\SOFTWARE\Policies\Microsoft\Cryptography\Configuration\SSL\00010002
  • HKLM\SYSTEM\CurrentControlSet\Control\Cryptography\Configuration\Local\SSL\00010002

这是我今天早些时候如何解决与您的问题非常相似的问题的完整文章:http://blog.jonschneider.com/2016/08/fix-ssl-handshaking-error-in-windows.html