16
votes

I have an incredibly simple web request with RestSharp:

var client = new RestClient("https://website.net");
var request = new RestRequest("/process", Method.GET);
request.AddParameter("cmd", "execute");
IRestResponse response = client.Execute(request);

var content = response.Content;
Console.WriteLine("Response: " + content);

This returns the error message:

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

Three things:

  1. I get the response I expect through a browser,
  2. I get the response I expect through Postman,
  3. This request is being sent to a test environment, but I can send it to a production environment, which has a very similar address, and get the response I expect,
  4. I'm positive it worked before today.

Their certificate is using TLS 1.2 with AES 128, so it is unrelated to errors caused by RC4.

This is on my local Win 10 machine in Visual Studio 2015 with a target framework of .NET 4.5.2.

Why do I get this error?

EDIT:

By changing my code to use the base System.Net and the WebRequest class and adding the line:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

as suggested from here, it works. So I guess RestSharp is using the incorrect protocol for some reason?

3
Is the website certificate valid? If not you would need to handle the CertificateCallback and validate manuallydsdel
Which .NET Framework/Version do you target? What's your OS? Does this happen on your (local dev) machine or the (production) server?wp78de
Take a look at Windows Event Viewer to get a more in-depth reason for the error than the generic message Windows provides. You should be able to get a TLS error code from there.ToastyMallows
I downloaded the source out of curiosity (never used RestSharp before). Internally it uses the System.Net.HttpWebRequest (which inherits WebRequest). I also came across the code that catches the exception internally and setting it as the response.ErrorMessage (so an exception is thrown somewhere). Is it possible that adding the line enabling TLS1.2 would work with the RestSharp code that you had?Wiz

3 Answers

12
votes

In .NET 4.5, TLS 1.2 is available but not enabled by default.

So yes, if you need TLS 1.2, you'll need to specify it in some way for the .NET run time.

FYI: In .NET 4.7, .NET will use the OS's default SSL/TLS version (most modern OS's will have TLS 1.2 as their default)


Other good SO Answer:

Default SecurityProtocol in .NET 4.5

5
votes

move this line: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

before this line: WebRequest request = WebRequest.Create(url);

Hope that helps.

4
votes

Even for Restsharp Library using the line below:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

before

IRestResponse response = client.Execute(request);

will work.