简体   繁体   English

.Net Framework 4.6.1 不默认为 TLS 1.2

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

Our client have recently upgrade the security protocol to TLS 1.2.我们的客户最近将安全协议升级到 TLS 1.2。 Therefore We have our application upgraded to 4.6.1 expecting the security protocol will be default to TLS 1.2 but it is not.因此,我们将应用程序升级到 4.6.1,期望安全协议默认为 TLS 1.2,但事实并非如此。 Any idea why?知道为什么吗?

I had a similar problem and this is what worked for me.我有一个类似的问题,这对我有用。

  1. open Powershell and check for supported protocols by using [Net.ServicePointManager]::SecurityProtocol打开 Powershell 并使用[Net.ServicePointManager]::SecurityProtocol检查支持的协议

  2. Run the following 2 cmdlets to set .NET Framework strong cryptography registry keys :运行以下 2 个 cmdlet 来设置.NET Framework 强加密注册表项

    set strong cryptography on 64 bit .Net Framework (version 4 and above)在 64 位 .Net Framework(版本 4 及更高版本)上设置强加密

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

    set strong cryptography on 32 bit .Net Framework (version 4 and above)在 32 位 .Net Framework(版本 4 及更高版本)上设置强加密

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

  3. Restart Powershell and check again for supported protocol by using [Net.ServicePointManager]::SecurityProtocol重新启动 Powershell 并使用[Net.ServicePointManager]::SecurityProtocol再次检查支持的协议

It should now display Tls12 as well.它现在也应该显示Tls12

Hope this helps希望这可以帮助

As others have mentioned there are a number of Windows Registry keys that must be set to enable TLS 1.2 in existing .NET applications without explicitly setting the protocol version in application code.正如其他人所提到的,必须设置许多 Windows 注册表项才能在现有 .NET 应用程序中启用 TLS 1.2,而无需在应用程序代码中明确设置协议版本。

In order to make .NET 4.x code select the strongest available protocol by default (ie when a protocol is not explicitly specified in code), the following registry keys are needed:为了使.NET 4.x 代码默认选择最强的可用协议(即当代码中未明确指定协议时),需要以下注册表项:

On 32-bit and 64-bit versions of Windows : HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\SchUseStrongCrypto: 0X00000001在 32 位和 64 位版本的 Windows 上HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\SchUseStrongCrypto: 0X00000001

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

The WOW6432Node value is needed to enable TLS 1.2 in 32-bit applications when run on 64-bit systems.在 64 位系统上运行时,需要WOW6432Node值才能在 32 位应用程序中启用 TLS 1.2。

But here's the quick and easy solution: https://github.com/TheLevelUp/pos-tls-patcher但这是快速简便的解决方案: https : //github.com/TheLevelUp/pos-tls-patcher

Update:更新:

If you're targetting .NET 4.6 or 4.7 you'll be interested in Transport Layer Security (TLS) best practices with the .NET Framework .如果您的目标是 .NET 4.6 或 4.7,您会对使用 .NET Framework 的传输层安全性 (TLS) 最佳实践感兴趣。

Note that TLS Patcher linked above very much follows the Microsoft recommendation for existing .NET 4.0 and 4.5 apps that cannot target .NET 4.6 or higher.请注意,上面链接的TLS Patcher非常遵循 Microsoft 对无法针对 .NET 4.6 或更高版本的现有 .NET 4.0 和 4.5 应用程序的建议。

The reason why the security protocol did not default to TLS 1.2 is because there is no default value for this in .NET Framework 4.6.1.安全协议没有默认为 TLS 1.2 的原因是因为在 .NET Framework 4.6.1 中没有默认值。 Sorry if this is reiterating what's already been said but I wanted to elaborate and I don't have enough reputation to comment.对不起,如果这是重申已经说过的话,但我想详细说明,我没有足够的声誉来发表评论。

There is no default value in 4.6.2 either, however like one of the commenters mentioned above, a console application does seem to default to TLS 1.2. 4.6.2 中也没有默认值,但是就像上面提到的一位评论者一样,控制台应用程序似乎默认使用 TLS 1.2。 I tried the exact same code in a website project targeting 4.6.2 and it did NOT default to TLS 1.2.我在一个针对 4.6.2 的网站项目中尝试了完全相同的代码,但它没有默认为 TLS 1.2。

4.7 and above does have a default value of SecurityProtocolType.SystemDefault. 4.7 及更高版本确实具有 SecurityProtocolType.SystemDefault 的默认值。

"This allows .NET Framework networking APIs based on SslStream (such as FTP, HTTP, and SMTP) to inherit the default security protocols from the operating system or from any custom configurations performed by a system administrator" “这允许基于 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 https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.securityprotocol?view=netframework-4.7#System_Net_ServicePointManager_SecurityProtocol

We experienced a similar problem while hosting our .NET 4.6.2 application in IIS.我们在 IIS 中托管 .NET 4.6.2 应用程序时遇到了类似的问题。

We could solve the problem by adding the httpRuntime element to the web.config.我们可以通过将httpRuntime元素添加到 web.config 来解决这个问题。 Without it our service did not default to TLS 1.2.没有它,我们的服务不会默认使用 TLS 1.2。

<httpRuntime targetFramework="4.6.2" />

For more info see https://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.httpruntimesection?view=netframework-4.7.2有关详细信息,请参阅https://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.httpruntimesection?view=netframework-4.7.2

MSDN: ServicePointManager.SecurityProtocol Property MSDN:ServicePointManager.SecurityProtocol 属性

This property selects the version of the Secure Sockets Layer (SSL) or Transport Layer Security (TLS) protocol to use for new connections that use the Secure Hypertext Transfer Protocol (HTTPS) scheme only;此属性选择用于仅使用安全超文本传输​​协议 (HTTPS) 方案的新连接的安全套接字层 (SSL) 或传输层安全 (TLS) 协议的版本; existing connections are not changed.现有连接不会改变。 Note that no default value is listed for this property, on purpose.请注意,此属性没有故意列出默认值。

The security landscape changes constantly, and default protocols and protection levels are changed over time in order to avoid known weaknesses.安全形势不断变化,默认协议和保护级别会随着时间的推移而变化,以避免已知的弱点。 Defaults will vary depending on individual machine configuration, and on which software is installed, and on which patches have been applied.默认值将根据个别机器配置、安装的软件以及应用的补丁而有所不同。

Taken from here取自这里

I faced the problem too.我也遇到了这个问题。 When local application tried to connect to a server that supports TLS 1.1 and TLS 1.2 it used to get "An existing connection was forcibly closed by the remote host" exception or when TLS 1.1/1.2 were not enabled properly it used to get "Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm"当本地应用程序尝试连接到支持 TLS 1.1 和 TLS 1.2 的服务器时,它曾经获得“远程主机强制关闭现有连接”异常,或者当 TLS 1.1/1.2 未正确启用时,它曾经获得“Win32Exception:客户端和服务器无法通信,因为它们没有共同的算法”

Below there are all registry keys and values that are needed for x64 windows OS.下面是 x64 Windows 操作系统所需的所有注册表项和值。 If you have 32bit OS (x86) just remove the last 2 lines.如果您有 32 位操作系统 (x86),只需删除最后 2 行。 TLS 1.0 will be disabled by the registry script. TLS 1.0 将被注册表脚本禁用。 Restarting OS is required.需要重新启动操作系统。

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

Based on the following link基于以下链接

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls

I've added我已经添加

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

to my code, and this fixed the issue for me.到我的代码,这为我解决了这个问题。 This is meant to default to the highest level the OS supports, which is the same behaviour you get by default from 4.7 and above.这意味着默认为操作系统支持的最高级别,这与默认情况下从 4.7 及更高版本获得的行为相同。

I did the following steps to use the latest security protocol TLS v.1.2:我执行了以下步骤来使用最新的安全协议 TLS v.1.2:

Disable the old protocols SSL2.0, SSL3.0, TLS1.0, TLS1.1, enable TLS1.2 and enable strong cryptography for .NET Framework in the registry.禁用旧协议 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

I used this in my code on the initial page.我在初始页面的代码中使用了它。 The app is web forms in VB.NET with .NET Framework 4.6.1该应用程序是带有 .NET Framework 4.6.1 的 VB.NET 中的 Web 表单

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

I tried the following code in my api which is not working for Kaltura account integration(bec'z Kaltura API upgraded to TLS 1.2) and it start working.我在我的 api 中尝试了以下代码,该代码不适用于 Kaltura 帐户集成(bec'z Kaltura API 升级到 TLS 1.2),它开始工作。 .Net framework is 4.5.2 and server :windows server 2008 r2 where application hosted. .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

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM