描述:請求被中止: 未能創建 SSL/TLS 安全通道。Could not create SSL/TLS secure channel。 産生平台:Windows Server 2012,Windows 7 Service Pack 1(SP1)和Windows Server 2008 R2 SP1
[插曲] 網站下載 是一款整站下載工具,輸入網址一鍵下載,操作簡單易用,多線程任務
在HttpWebRequest前設置代碼
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
複製下面注冊表代碼導入到注冊表。新建txt,将後綴txt改爲reg(注冊表項),導入(導入之前做備份)
[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
[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
PowerShell打開:
[Net.ServicePointManager]::SecurityProtocol [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3 -bor [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
第一行代碼檢查支持TLS版本 第二行代碼修改TLS支持
前面兩種辦法都不行,隻能使用終極辦法:
備注:具體到每個windows版本對應支持的TSL版本,請參考本文中的補充資料部分内容。
存在解決方案,但是它們取決於框架版本:
.NET 4.6及更高版本。您無需執行任何其他工作即可支持TLS 1.2,默認情況下支持該功能。
.NET 4.5。支持TLS 1.2,但它不是默認協議。您需要選擇使用它。以下代碼将TLS 1.2設置爲默認值,請確保在連接到安全資源之前執行它:
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的條目,因此我們必須使用此枚舉值的數字表示形式:
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支持。
請參閱:
可能網站提供的證書密鑰長度爲512位,按目前的行業標準應包含不小於2048位的公鑰。 微軟在2016年9月的安全更新響應這一問題,如果公鑰長度小於2048字節(例如RSA 512),Windows可以取消HTTPS連接
2012 R2 and Windows 8
2008 R2 and Windows 7
SecurityProtocolType.Tls1.0=0xC0; SecurityProtocolType.Tls1.1=0x300; SecurityProtocolType.Tls1.2=0xC00;
.net 4.0/4.5默認值:SecurityProtocolType.Tls | SecurityProtocolType.Ssl3 .net 4.6/4.7默認值:SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12
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 在Win7Sp1和.Net 3.5.1中,支持TLS1.2
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3; //關閉ssl3 ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0x300 | (SecurityProtocolType)0xc00; //增加1.1和1.2支持
TLS1.2的結論是這樣的:
.net4下的注冊表修改
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
本文由網站下載收集整理,内容來源於互聯網,轉載請注明出處,謝謝。