解决无法建立SSL / TLS安全通道(The request was aborted: Could not create SSL/TLS secure channel)的问题

解决无法建立SSL / TLS安全通道(The request was aborted: Could not create SSL/TLS secure channel)的问题

这个问题主要是没配置好SSL版本

在谷歌浏览器中打开该链接,按下F12,打开开发者工具,切换到Security选项卡,就可以看到了。这一串说明:
The connection to this site is encrypted and authenticated using TLS 1.2, ECDHE_RSA with P-256, and AES_128_GCM.
就是 TLS1.2
各SSL版本与C#代码的对应:
| SSL/TLS版本 | 对应C#代码 |
| :——–: | :————-: |
| SSL 3 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; |
| TLS 1.0 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; |
| TLS 1.1 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; |
| TLS 1.2 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |

在发起https请求前使用以下代码

1
2
3
4
5
6
7
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
//如果编译器报错:未包含"Tls11"的定义,用以下代码,要解决这个报错也可以安装.net framework 4.5
//System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | (SecurityProtocolType)3072 | (SecurityProtocolType)768 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // 总是接受
return true;
});//验证服务器证书回调自动验证

原文链接:http://blog.albsz.cn/2020-11-05-sslTlsFail.html

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

撸代码不易,给点物质上的支持吧~

支付宝
微信