当前位置:   article > 正文

解决dotnet调用https请求被中止未能创建SSL/TLS安全通道_命令行[net.servicepointmanager]::securityprotocol

命令行[net.servicepointmanager]::securityprotocol

环境: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;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

第二试

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

statckoverflow 上面大多答案如下:

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
  • 1

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  • 1

原因:

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
  • 1

打印出来的结果应该是不包含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
  • 1

针对32位.Net:

Set-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft.NetFramework\v4.0.30319’ -Name ‘SchUseStrongCrypto’ -Value ‘1’ -Type DWord
关闭PowerShell窗口,打开一个新的PowerShell窗口,执行以下代码

[Net.ServicePointManager]::SecurityProtocol
  • 1

结果如下:

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之后不会立即生效的,重启机器后生效。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号