Our client have recently upgrade the security protocol to 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. Any idea why?
11 Answers
I had a similar problem and this is what worked for me.
open Powershell and check for supported protocols by using
[Net.ServicePointManager]::SecurityProtocol
Run the following 2 cmdlets to set .NET Framework strong cryptography registry keys:
set strong cryptography on 64 bit .Net Framework (version 4 and above)
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)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Restart Powershell and check again for supported protocol by using
[Net.ServicePointManager]::SecurityProtocol
It should now display Tls12
as well.
Hope this helps
-
This should most certainly work on .NET 4.x application code with .NET 4.6+ installed. Mar 2, 2018 at 5:09
-
2Worked for me - Thank you! By the way, if time, please add a backslash between Microsoft and .NetFramework for others: "Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord" .. Both commands are also the same. Perhaps a note could be made to this effect. Thanks again.– sean2078Mar 17, 2018 at 10:03
-
1Thanks for the comments. The 64-bit should also include the Wow6432node key. Will fix. Mar 22, 2018 at 2:26
-
-
3Note that for me, with .NET 4.7.2 installed, the
SecurityProtocol
returnedSystemDefault
both before and after executing the two commands. However, this did still work!– RichardMNov 4, 2019 at 20:58
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.
In order to make .NET 4.x code select the strongest available protocol by default (i.e. when a protocol is not explicitly specified in code), the following registry keys are needed:
On 32-bit and 64-bit versions of 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
The WOW6432Node value is needed to enable TLS 1.2 in 32-bit applications when run on 64-bit systems.
But here's the quick and easy solution: 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.
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.
-
6I think this should be the accepted answer. We faced the same problem and observed the following: A normal .Net EXE works as expected. An assembly hosted in a native process (e.g. IIS, Office Addin) only used TLS 1.2 if the registry settings were made. Mar 8, 2018 at 14:45
-
@StefanEgli I feel your pain. Not sure why this info is not documented better. We are trying to get the word out: blog.thelevelup.com/pci-security-is-your-restaurant-ready Mar 8, 2018 at 14:52
-
@StefanEgli also if you want TLS 1.2 in native code you also need Schannel registry keys set: docs.microsoft.com/en-us/windows-server/security/tls/… Mar 8, 2018 at 15:08
-
1The \4.0.30319 portion of the key is incorrect and should be \v4.0.30319– JakeJun 17, 2019 at 21:39
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. 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. I tried the exact same code in a website project targeting 4.6.2 and it did NOT default to TLS 1.2.
4.7 and above does have a default value of 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"
We experienced a similar problem while hosting our .NET 4.6.2 application in IIS.
We could solve the problem by adding the httpRuntime
element to the web.config. Without it our service did not default to 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
-
Nice find! This solved it for us as well since we didn't have it set. Jan 10, 2020 at 18:21
MSDN: ServicePointManager.SecurityProtocol Property
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; 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
-
The default Security Protocol for .NET 4.6.1 is not TLS 1.2 - You have to set the value of the ServicePointManager.SecurityProtocol to it to add support May 9, 2017 at 15:02
-
How does this answer the question? The OP says that TLS1.2 didn't work when it should May 9, 2017 at 15:02
-
I understand the question as "I did nothing and expected TLS 1.2 worked" May 9, 2017 at 15:03
-
1The answer isn't relevant. The comment maybe, if it was accompanied with a reference. In 4.6.2 TLS1.2 is enabled by default May 9, 2017 at 15:05
-
1
Based on the following link
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.
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"
Below there are all registry keys and values that are needed for x64 windows OS. If you have 32bit OS (x86) just remove the last 2 lines. TLS 1.0 will be disabled by the registry script. 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
I used this in my code on the initial page. The app is web forms in VB.NET with .NET Framework 4.6.1
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
-
2This actually overwrites all other versions, not to mention that it does not answer the question.– kuskmenMar 7, 2018 at 23:15
-
2Please do not hardcode the security protocol in application code. You want to follow the recommendations in the answers above, which are also inline with Microsoft TLS Best Practices with .NET Mar 16, 2018 at 2:01
-
Odd. I had a code base started in Framework 3.5 that shared a dll from both a website and an .exe. The dll, website, and .exe were all upgraded to Framework 4.6.2. The .exe worked fine and used TLS 1.2. The website did not. I had to explicitly designate the protocol version per this post to get the website to work. May 7, 2018 at 13:00
I did the following steps to use the latest security protocol 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.
[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 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. .Net framework is 4.5.2 and server :windows server 2008 r2 where application hosted.
#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.
- Update Windows and WinHTTP
- Ensure that TLS 1.2 is enabled as a protocol for SChannel at the operating system level
- Update and configure the .NET Framework to support TLS 1.2
The request was aborted: Could not create SSL/TLS secure channel.
.