Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Client in .NET 4.5.1: How to enable TLS 1.2 when WebRequest is used?

Tags:

.net

wcf

Our .net WCF Client, the WebRequest call, compiled to a windows EXE, running on Win2012-R2, refuses to connect to a web server that surfaces ONLY TLS 1.2

We know that Win2012 and .NET 4.5x support TLS 1.2

We have no problems when the server surfaces TLS 1.0 and up. The problem is only seen when the server we connect to has DISABLED TLS 1.0, 1.1 and SSL2 and SSL3. The Server ONLY surfaces TLS 1.2. Chrome and firefox (on Win 7 and higher) connect fine to the server (no warnings or SSL issues of any kind).

The server certificate is %100 OK.

The problem is that WebRequest fails to connect in this situation.

What do we need to set in code so that our use of WebRequest will connect to systems that may run TLS 1.2, 1.1, 1.0, and/or SSL v3?

like image 897
Jonesome Reinstate Monica Avatar asked Apr 16 '15 01:04

Jonesome Reinstate Monica


People also ask

How do I enable TLS 1.2 in Visual Studio?

How to enable TLS 1.2. The easiest way to avoid these issues is to upgrade to the latest version of Visual Studio as it already uses TLS 1.2 for all HTTPS connections. If upgrading Visual Studio is not an option, you can set a set a machine-wide registry key to enable TLS 1.2 on all .

Does WCF use TLS?

When running the installer, you will see that when making the WCF call during installation it will only use TLS 1.0, but when you run the installed app afterwards, it will use TLS 1.2. Even though it is the exact same code base that makes the WCF call.


2 Answers

You should work with .NET 4.5 or above version and add this line in your code:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 
like image 124
Yujie Avatar answered Sep 21 '22 08:09

Yujie


While not easy to figure out, the needed property is:

System.Net.ServicePointManager.SecurityProtocol

This can be used to disable and enable TLS levels in the WCF environment.

Further, you can see what WCF is currently set to using:

Console.WriteLine(System.Net.ServicePointManager.SecurityProtocol.ToString()); 

With thanks to: How do I disable SSL fallback and use only TLS for outbound connections in .NET? (Poodle mitigation)

like image 21
Jonesome Reinstate Monica Avatar answered Sep 19 '22 08:09

Jonesome Reinstate Monica