簡體   English   中英

.Net Framework 4.6.1 不默認為 TLS 1.2

[英].Net Framework 4.6.1 not defaulting to TLS 1.2

我們的客戶最近將安全協議升級到 TLS 1.2。 因此,我們將應用程序升級到 4.6.1,期望安全協議默認為 TLS 1.2,但事實並非如此。 知道為什么嗎?

我有一個類似的問題,這對我有用。

  1. 打開 Powershell 並使用[Net.ServicePointManager]::SecurityProtocol檢查支持的協議

  2. 運行以下 2 個 cmdlet 來設置.NET Framework 強加密注冊表項

    在 64 位 .Net Framework(版本 4 及更高版本)上設置強加密

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

    在 32 位 .Net Framework(版本 4 及更高版本)上設置強加密

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

  3. 重新啟動 Powershell 並使用[Net.ServicePointManager]::SecurityProtocol再次檢查支持的協議

它現在也應該顯示Tls12

希望這可以幫助

正如其他人所提到的,必須設置許多 Windows 注冊表項才能在現有 .NET 應用程序中啟用 TLS 1.2,而無需在應用程序代碼中明確設置協議版本。

為了使.NET 4.x 代碼默認選擇最強的可用協議(即當代碼中未明確指定協議時),需要以下注冊表項:

在 32 位和 64 位版本的 Windows 上HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\SchUseStrongCrypto: 0X00000001

在 64 位版本的 Windows 上HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\.NETFramework\\v4.0.30319\\SchUseStrongCrypto: 0X00000001

在 64 位系統上運行時,需要WOW6432Node值才能在 32 位應用程序中啟用 TLS 1.2。

但這是快速簡便的解決方案: https : //github.com/TheLevelUp/pos-tls-patcher

更新:

如果您的目標是 .NET 4.6 或 4.7,您會對使用 .NET Framework 的傳輸層安全性 (TLS) 最佳實踐感興趣。

請注意,上面鏈接的TLS Patcher非常遵循 Microsoft 對無法針對 .NET 4.6 或更高版本的現有 .NET 4.0 和 4.5 應用程序的建議。

安全協議沒有默認為 TLS 1.2 的原因是因為在 .NET Framework 4.6.1 中沒有默認值。 對不起,如果這是重申已經說過的話,但我想詳細說明,我沒有足夠的聲譽來發表評論。

4.6.2 中也沒有默認值,但是就像上面提到的一位評論者一樣,控制台應用程序似乎默認使用 TLS 1.2。 我在一個針對 4.6.2 的網站項目中嘗試了完全相同的代碼,但它沒有默認為 TLS 1.2。

4.7 及更高版本確實具有 SecurityProtocolType.SystemDefault 的默認值。

“這允許基於 SslStream 的 .NET Framework 網絡 API(例如 FTP、HTTP 和 SMTP)從操作系統或系統管理員執行的任何自定義配置繼承默認安全協議”

https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.securityprotocol?view=netframework-4.7#System_Net_ServicePointManager_SecurityProtocol

我們在 IIS 中托管 .NET 4.6.2 應用程序時遇到了類似的問題。

我們可以通過將httpRuntime元素添加到 web.config 來解決這個問題。 沒有它,我們的服務不會默認使用 TLS 1.2。

<httpRuntime targetFramework="4.6.2" />

有關詳細信息,請參閱https://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.httpruntimesection?view=netframework-4.7.2

MSDN:ServicePointManager.SecurityProtocol 屬性

此屬性選擇用於僅使用安全超文本傳輸​​協議 (HTTPS) 方案的新連接的安全套接字層 (SSL) 或傳輸層安全 (TLS) 協議的版本; 現有連接不會改變。 請注意,此屬性沒有故意列出默認值。

安全形勢不斷變化,默認協議和保護級別會隨着時間的推移而變化,以避免已知的弱點。 默認值將根據個別機器配置、安裝的軟件以及應用的補丁而有所不同。

取自這里

我也遇到了這個問題。 當本地應用程序嘗試連接到支持 TLS 1.1 和 TLS 1.2 的服務器時,它曾經獲得“遠程主機強制關閉現有連接”異常,或者當 TLS 1.1/1.2 未正確啟用時,它曾經獲得“Win32Exception:客戶端和服務器無法通信,因為它們沒有共同的算法”

下面是 x64 Windows 操作系統所需的所有注冊表項和值。 如果您有 32 位操作系統 (x86),只需刪除最后 2 行。 TLS 1.0 將被注冊表腳本禁用。 需要重新啟動操作系統。

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Client]
"DisabledByDefault"=dword:00000001
"enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\server]
"disabledbydefault"=dword:00000001
"enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\ssl 3.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\ssl 3.0\client]
"disabledbydefault"=dword:00000001
"enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\ssl 3.0\server]
"disabledbydefault"=dword:00000001
"enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\tls 1.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\tls 1.0\client]
"disabledbydefault"=dword:00000001
"enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\tls 1.0\server]
"disabledbydefault"=dword:00000001
"enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\tls 1.1]

[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]

[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

[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://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls

我已經添加

AppContext.SetSwitch("Switch.System.Net.DontEnableSystemDefaultTlsVersions", false);

到我的代碼,這為我解決了這個問題。 這意味着默認為操作系統支持的最高級別,這與默認情況下從 4.7 及更高版本獲得的行為相同。

我執行了以下步驟來使用最新的安全協議 TLS v.1.2:

禁用舊協議 SSL2.0、SSL3.0、TLS1.0、TLS1.1,啟用 TLS1.2 並在注冊表中為 .NET Framework 啟用強加密。

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Client]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Server]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Client]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Server]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\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\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001

我在初始頁面的代碼中使用了它。 該應用程序是帶有 .NET Framework 4.6.1 的 VB.NET 中的 Web 表單

System.Net.ServicePointManager.SecurityProtocol =  System.Net.SecurityProtocolType.Tls12

我在我的 api 中嘗試了以下代碼,該代碼不適用於 Kaltura 帳戶集成(bec'z Kaltura API 升級到 TLS 1.2),它開始工作。 .Net 框架是 4.5.2 和服務器:應用程序托管的 windows server 2008 r2。

#region To handle TLS (Transport Layer Security) Version and support
            var assembly = Assembly.GetExecutingAssembly();
            var attributes = assembly.GetCustomAttributes(typeof(TargetFrameworkAttribute), false);
            var version = (TargetFrameworkAttribute)attributes[0];

            SecurityProtocolType flag;
            if (Enum.TryParse("Tls11", out flag))
                ServicePointManager.SecurityProtocol |= flag;
            if (Enum.TryParse("Tls12", out flag))
                ServicePointManager.SecurityProtocol |= flag;
#endregion

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM