解决dotnet调用https请求被中止未能创建SSL/TLS安全通道

环境:dotnet4.7.2/winserver2012

问题描述:

调用https出现请求被中止,未能创建SSL/TLS安全通道
在这里插入图片描述

第一试

自定义SetCertificatePolicy 函数,在建立http连接之前调用 SetCertificatePolicy 函数。

 public static void SetCertificatePolicy() {
            ServicePointManager.ServerCertificateValidationCallback
                += RemoteCertificateValidate;
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.SystemDefault |
                                                   SecurityProtocolType.Tls | SecurityProtocolType.Tls11 |
                                                   SecurityProtocolType.Tls12;
        }

        private static bool RemoteCertificateValidate(object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error) {
            // trust any certificate!!!
            //System.Console.WriteLine("Warning, trust any certificate");
            return true;
        }

第二试

请求被中止: 未能创建 SSL/TLS 安全通道
英文搜索关键词 : the request was aborted could not create ssl/tls (net 4.6)

statckoverflow 上面大多答案如下:

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

原因:

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

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

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

附加资料

查看服务器是否可以升为TLS1.2
在这里插入图片描述

网上有手动改注册表的方案,但这种方式万一误操作就不好了。所以还是建议使用软件修改,推荐下面这个软件。

官方网站地址:https://www.nartac.com/Products/IISCrypto/
下载地址:https://www.nartac.com/Products/IISCrypto/Download
下载地址:window服务器禁用默认的ssl2.0和ssl3.0只启用启用tls1.2保证安全

在这里插入图片描述

对了,如果是新版的是有个Reboot可选,表示重启机器,这个要勾选。Apply之后不会立即生效的,重启机器后生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alvinToffler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值