.Net无法创建SSL / TLS安全通道

时间:2018-01-24 10:01:24

标签: c# .net ssl

我将.net应用程序中的xml发布到第三方Web服务但接收到无法创建SSL / TLS安全通道的#34;错误。当我使用soapUI发出请求时它工作正常,我得到了回复。但似乎无法从我的.net控制台应用程序中获取它。

我已尝试将安全性设置为tls1和tls12,但仍未成功。证书安装在我发出这些请求的服务器上。

有没有人设法解决这个问题?

以下是我的代码示例

 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://thirdPartyURL/cgi-bin/XmlProc");
byte[] bytes;
             bytes = System.Text.Encoding.ASCII.GetBytes("myXML");
            request.ContentType = "text/xml; encoding='utf-8'"; 
            request.ContentLength = bytes.Length;
            request.Method = "POST";                
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();

1 个答案:

答案 0 :(得分:0)

通过执行以下操作解决了此问题:

  1. 将.Net Framework更改为4.5
  2. 在“第三方根用户”商店中安装证书
  3. 在我的应用程序中发出请求之前添加了这行代码

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

  4. 在Windows Server 2012而不是Windows Server 2008上运行应用程序。可能是Windows Server 2008不支持TLS v1.2的情况