Loading color scheme

How to Enable TLS v1.2 in .NET Framework 4.0

Sometimes you need to use SSL/TLS protocol version 1.2 in your existing .NET 4.0 C# applications. The trick is that by default .NET 4.0 only supports TLS v1.0 and there is no standard way of forcing to use never TLS version. Why may you need a newer TLS version? Because some REST or WebService API providers have switched to using TLS v1.2 as a more secure and up-to-date protocol. 

When you get the following error, this may be also because of the outdated TLS version.

The request was aborted: Could not create SSL/TLS secure channel.

So, the solution to this is quite easy. First of all, please make sure you have the latest .NET framework version installed on the computer. Download and install the official software package of .NET framework from the Microsoft website. 

Second, add the following code to your Program.Main() function:


static void Main(string[] args)
{
    //this will enable TLS v 1.1 and 1.2
    System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)(768 | 3072);
			
    // your code here

}

 

What do these constants mean? Constant 768 means TLS 1.1 security protocol, and constant value 3072 means TLS 1.2 respectively.

The constant values are taken from here.

 

Get all interesting articles to your inbox
Please wait