Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not create SSL/TLS secure channel, despite setting ServerCertificateValidationCallback

Tags:

c#

https

ssl

I'm trying to establish SSL/TLS connection to test server with self-signed certificate. Communication through unsecure channel worked without issues.

Here is my sample code, which I've written based on this solutions: Allowing Untrusted SSL Certificates with HttpClient C# Ignore certificate errors? .NET client connecting to ssl Web API

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;  var c = new HttpClient(); var r = c.GetAsync("https://10.3.0.1:8443/rest/v1").Result; if (r.IsSuccessStatusCode) {     Log.AddMessage(r.Content.Get<string>()); } else {     Log.AddMessage(string.Format("{0} ({1})", (int)r.StatusCode, r.ReasonPhrase)); } 

also tried this:

var handler = new WebRequestHandler(); handler.ServerCertificateValidationCallback = delegate { return true; }; var c = new HttpClient(handler); ... 

and this

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

but each time I've got an exception:

InnerException: System.Net.Http.HttpRequestException    _HResult=-2146233088    _message=An error occurred while sending the request.    HResult=-2146233088    IsTransient=false    Message=An error occurred while sending the request.    InnerException: System.Net.WebException         _HResult=-2146233079         _message=The request was aborted: Could not create SSL/TLS secure channel.         HResult=-2146233079         IsTransient=false         Message=The request was aborted: Could not create SSL/TLS secure channel.         Source=System         StackTrace:              at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)              at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)         InnerException:  

What do I do wrong? Why I can't connect to this server (which has invalid-self-signed certificate)

like image 471
Marcin Konrad Ceglarek Avatar asked Oct 07 '15 14:10

Marcin Konrad Ceglarek


People also ask

Can not create SSL TLS secure channel?

The error “The request was aborted: Could not create SSL/TLS secure channel.” can happen during any download HTTP request. This error generally will correspond to firewalls, proxies or DNS filtering blocking the connection or an SSL/TLS cipher misconfiguration.

Can't create SSL TLS secure channel IIS?

However, the "Could not create SSL/TLS secure channel" error usually means that there's something wrong with the server certificate, e.g. the certificate is for a different hostname, or otherwise invalid, or not trusted etc.

Can't create SSL TLS secure channel excel?

Steps to Reproduce:Open the Microsoft Excel and check the version using Account ( In this case I was testing with Microsoft Excel 2016) Open the Microsoft Excel --> Data --> From Other Source --> From Odata Data Feed. Fill in the connection which can be found from Step 2. Below error is shown.

What is SSL TLS secure channel?

SSL/TLS creates a secure channel between a users' computer and other devices as they exchange information over the internet, using three main concepts: encryption, authentication, and integrity to accomplish this. Encryption hides data being transferred from any third parties.


2 Answers

You are doing it right with ServerCertificateValidationCallback. This is not the problem you are facing. The problem you are facing is most likely the version of SSL/TLS protocol.

For example, if your server offers only SSLv3 and TLSv10 and your client needs TLSv12 then you will receive this error message. What you need to do is to make sure that both client and server have a common protocol version supported.

When I need a client that is able to connect to as many servers as possible (rather than to be as secure as possible) I use this (together with setting the validation callback):

  ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 
like image 174
Wapac Avatar answered Sep 21 '22 06:09

Wapac


We have been solving the same problem just today, and all you need to do is to increase the runtime version of .NET

4.5.2 didn't work for us with the above problem, while 4.6.1 was OK

If you need to keep the .NET version, then set

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 
like image 26
martinh_kentico Avatar answered Sep 21 '22 06:09

martinh_kentico