请求被中止: 未能创建 SSL/TLS 安全通道

英文搜索关键词 : the request was aborted could not create ssl/tls (net 4.6)

statckoverflow 上面大多答案如下:

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

也有用如下的(强烈不建议使用)

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

原因:

  1. Tls1.0 由于安全原因,大部分https服务端已经不再建议支持。

  2. .Net 4.5及之前的版本,不支持Tls1.2 ,SecurityProtocolType这个枚举中没有Tls12这个项

  3. Windows Server 2012 R2中,安装了.Net 4.6.x或4.7.x后,虽然系统支持,.Net也支持,但是默认的协商协议列表不包含Tls1.2

解决方案(PowerShell脚本):

查看当前系统中默认启用的协商协议列表

[Net.ServicePointManager]::SecurityProtocol

打印出来的结果应该是不包含Tls12 的,我实测的Win10 1603以及Windows Server 2012 R2显示均为如下:

Ssl3, Tls

继续执行如下代码,修改设置:

针对64位.Net:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

针对32位.Net:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

关闭PowerShell窗口,打开一个新的PowerShell窗口,执行以下代码

[Net.ServicePointManager]::SecurityProtocol

结果如下:

Tls, Tls11, Tls12, Tls13

重启之前受影响的.Net应用程序即可生效

.Net <4.5.x 参考链接 https://stackoverflow.com/questions/27953681/changing-security-protocol-per-request-httpclient

.Net 4.6.x参考链接 https://trailheadtechnology.com/solving-could-not-create-ssl-tls-secure-channel-error-in-net-4-6-x/

官方资料:https://docs.microsoft.com/en-us/security/solving-tls1-problem


ps: 然后以上处理并没有解决我的问题。。。o(╥﹏╥)o

在生产机器上多次curl测试,发现偶尔才会出现,并非必现。于是我很机智的,在出现此异常时重试三次。最终搞定了!

你可能感兴趣的:(请求被中止: 未能创建 SSL/TLS 安全通道)