【问题标题】:TLS 1.2 Error With C# .Net : The underlying connection was closed: An unexpected error occurred on a sendC# .Net 的 TLS 1.2 错误:底层连接已关闭:发送时发生意外错误
【发布时间】:2016-04-20 21:57:21
【问题描述】:

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

当我从 .net c# 代码请求时收到此错误。

"The underlying connection was closed: An unexpected error occurred on a send."

这是我的代码;

    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;
    }
}

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

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

A fatal alert was received from the remote endpoint. The TLS protocol defined fatal alert code is 40.

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

感谢您的帮助!

【问题讨论】:

  • 在故障开始前不久,您的服务器或应用程序中是否有任何变化?是否应用了任何 Windows 更新?你确定问题不在银行这边吗?
  • 我确信问题不在于银行方面。如果问题出在银行方面,如何使用成功的 chrome 和 firefox。我高银行的支持和 ms14-066 更新最后一山。我最后一次成功的交易日期是 12 月 5 日。
  • 如果您上个月进行了更新,并且距离上一次成功交易已经过去一个月,那么您可能希望将更新回滚到以前的版本,看看是否有效。跨度>
  • 我做了一些谷歌搜索,看起来 ms14-066 是一个拙劣的补丁,破坏了一些 Schannel 功能。卸载更新。
  • 银行在上次安装时执行了更新,我不想回滚。我怎样才能超越这个问题,问题是什么。问题出在哪里,我应该去哪里看?

标签: c# asp.net ssl webclient tls1.2


【解决方案1】:

在服务器上安装 TLS1.2 后我也遇到了同样的问题;解决方案是在 Web 应用程序的情况下在 Application_Start() 函数中添加以下代码行,或者在 Windows 应用程序的情况下在 Program.cs 文件的 Main 方法中添加以下代码行,如下所示:

网络应用程序:

protected void Application_Start()
    {
        //Add this line
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        //End
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    }

Windows 应用程序:

[STAThread]
    static void Main()
    {
        //Add this line
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        //End
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Default());
    }

【讨论】:

    【解决方案2】:

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

    Chrome 和 Firefox(显然)有自己的一组 cipher_suites 值,这些值独立于您的操作系统的配置值,这就是为什么当 Internet Explorer 没有时它们仍然可以工作的原因。

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

    希望您会看到两条客户端问候消息之间的一些差异,这将使您能够查明失败的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2020-12-02
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多