# “请求被中止: 未能创建 SSL/TLS 安全通道”的原因及解决办法 > 描述:请求被中止: 未能创建 SSL/TLS 安全通道。Could not create SSL/TLS secure channel。 产生平台:Windows Server 2012,Windows 7 Service Pack 1(SP1)和Windows Server 2008 R2 SP1 ## 解决办法一:代码层面 在HttpWebRequest前设置代码 ```c# ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; ``` ## 解决办法二:系统层面 > 上面的方法没有效果,则为系统层面的问题了,对照你现在使用的系统更新系统补丁 更新以将TLS 1.1和TLS 1.2启用为Windows中WinHTTP中的默认安全协议,此更新提供对Windows Server 2012,Windows 7 Service Pack 1(SP1)和Windows Server 2008 R2 SP1中的传输层安全性(TLS)1.1和TLS 1.2的支持, 参考官方文档 [https://support.microsoft.com/en-us/help/3140245/update-to-enable-tls-1-1-and-tls-1-2-as-default-secure-protocols-in-wi](/link?id=554500000064525498db08d90a346844) 更新补丁KB3140245 * [http://www.catalog.update.microsoft.com/search.aspx?q=kb3140245](/link?id=5545000000645254993d08d90a346844) 在SChannel组件级别的Windows 7上启用TLS 1.1和1.2 (采用以下任意一种更新) ### 方法一 使用微软更新安装包更新 MicrosoftEasyFix51044.msi * 微软安装更新注册表:[http://download.microsoft.com/download/0/6/5/0658B1A7-6D2E-474F-BC2C-D69E5B9E9A68/MicrosoftEasyFix51044.msi](/link?id=5545000000645254998a08d90a346844) ### 方法二 手动更新注册表 > 复制下面注册表代码导入到注册表。新建txt,将后缀txt改为reg(注册表项),导入(导入之前做备份) WIN7 64 ```bash [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp] "DefaultSecureProtocols"=dword:00000a00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp] "DefaultSecureProtocols"=dword:00000a00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings] "SecureProtocols"=dword:00000a80 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "SecureProtocols"=dword:00000a80 ``` Windows Server ```bash [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp] "DefaultSecureProtocols"=dword:00000800 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp] "DefaultSecureProtocols"=dword:00000800 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client] "DisabledByDefault"=dword:00000000 "Enabled"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Server] "DisabledByDefault"=dword:00000000 "Enabled"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client] "DisabledByDefault"=dword:00000000 "Enabled"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server] "DisabledByDefault"=dword:00000000 "Enabled"=dword:00000001 ``` 验证系统是否支持TLS1.2、TLS1.3 PowerShell打开: ```bash [Net.ServicePointManager]::SecurityProtocol [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3 -bor [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12 ``` 第一行代码检查支持TLS版本 第二行代码修改TLS支持 ## 解决办法三:升级系统 > 前面两种办法都不行,只能使用终极办法: * 升级系统到windows 10和windows server 2019 (支持到TLS1.2); * 升级系统到windows 11和windows server 2022 (支持到TLS1.3)。 > 备注:具体到每个windows版本对应支持的TSL版本,请参考本文中的补充资料部分内容。 * * * ## 其他参考内容 ### 其他参考内容一 * [https://blogs.perficient.com/2016/04/28/tsl-1-2-and-net-support/](/link?id=554500000064525499ca08d90a346844) 存在解决方案,但是它们取决于框架版本: .NET 4.6及更高版本。您无需执行任何其他工作即可支持TLS 1.2,默认情况下支持该功能。 .NET 4.5。支持TLS 1.2,但它不是默认协议。您需要选择使用它。以下代码将TLS 1.2设置为默认值,请确保在连接到安全资源之前执行它: ```bash ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 ``` .NET 4.0。不支持TLS 1.2,但是如果系统上安装了.NET 4.5(或更高版本),那么即使您的应用程序框架不支持TLS 1.2,您仍然可以选择使用TLS 1.2。唯一的问题是.NET 4.0中的SecurityProtocolType没有TLS1.2的条目,因此我们必须使用此枚举值的数字表示形式: ```bash ServicePointManager.SecurityProtocol =(SecurityProtocolType)3072; ``` .NET 3.5或更低版本。不支持TLS 1.2(\*),并且没有解决方法。将您的应用程序升级到该框架的最新版本。 PS对于场景3,还有一个注册表黑客,默认情况下会强制4.5使用TLS 1.2,而无需以编程方式强制执行。 PPS正如Microsoft的Christian Pop在下面提到的那样,.NET 3.5有一个可用的最新补丁程序,它启用了TLS1.2支持。 请参阅: * KB3154518 – Reliability Rollup HR-1605 – NDP 2.0 SP2 – Win7 SP1/Win 2008 R2 SP1 * KB3154519 – Reliability Rollup HR-1605 – NDP 2.0 SP2 – Win8 RTM/Win 2012 RTM * KB3154520 – Reliability Rollup HR-1605 – NDP 2.0 SP2 – Win8.1RTM/Win 2012 R2 RTM * KB3156421 -1605 HotFix Rollup through Windows Update for Windows 10. (adsbygoogle = window.adsbygoogle || \[\]).push({}); ### 其他参考内容二 可能网站提供的证书密钥长度为512位,按目前的行业标准应包含不小于2048位的公钥。 微软在2016年9月的安全更新响应这一问题,如果公钥长度小于2048字节(例如RSA 512),`Windows可以取消HTTPS连接` 安装的更新有 2012 R2 and Windows 8 * KB3185331 * KB3188743 * KB3174644 2008 R2 and Windows 7 * KB3185278 * KB3185330 * KB3192391 * KB3175024 * KB3172605 ### 其他参考内容三 ```bash SecurityProtocolType.Tls1.0=0xC0; SecurityProtocolType.Tls1.1=0x300; SecurityProtocolType.Tls1.2=0xC00; ``` ```bash .net 4.0/4.5默认值:SecurityProtocolType.Tls | SecurityProtocolType.Ssl3 .net 4.6/4.7默认值:SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 ``` [https://support.microsoft.com/en-us/help/3069494/cannot-connect-to-a-server-by-using-the-servicepointmanager-or-sslstre](/link?id=55450000006452547f5308da010a69fd) SCH\_USE\_STRONG\_CRYPTO 此标志将在.NET Framework 4.6中自动使用 [https://support.microsoft.com/en-us/help/3154518/support-for-tls-system-default-versions-included-in-the-net-framework](/link?id=55450000006452547fa108da010a69fd) 在Win7Sp1和.Net 3.5.1中,支持TLS1.2 ```bash ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3; //关闭ssl3 ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0x300 | (SecurityProtocolType)0xc00; //增加1.1和1.2支持 ``` TLS1.2的结论是这样的: * 安装.Net3.5.1需要打个补丁,然后增加TLS1.2枚举 * 安装.Net 4.0需要修改注册表,然后增加TLS1.2枚举 * 安装.Net4.5之后,还需要增加TLS1.2枚举 * 安装.Net4.6.1之后,默认支持TLS1.2 .net4下的注册表修改 ```bash Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319] "SchUseStrongCrypto"=dword:00000001 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319] "SchUseStrongCrypto"=dword:00000001 ``` ## 快照 - https://pic.rmb.bdstatic.com/bjh/240926/8a4897c4981cc1ef028b0bcca6b11f82153.png - https://i3.wp.com/pic.rmb.bdstatic.com/bjh/240926/8a4897c4981cc1ef028b0bcca6b11f82153.png