Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ignore https certificate warnings in the c# signalr client?

Tags:

c#

signalr

I'm attempting to connect to a SignalR server with an invalid certificate. Unsurprisingly I get the following error:

    System.Net.Http.HttpRequestException : An error occurred while sending the request.
----> System.Net.WebException : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
  ----> System.Security.Authentication.AuthenticationException : The remote certificate is invalid according to the validation procedure.

With the normal .Net HttpClient you can construct it with a WebRequestHandler that has a ServerCertificateValidationCallback delegate, allowing you to change the certificate validation behaviour. The SignalR HttpClient appears to have none of this.

like image 687
Dan Avatar asked Feb 24 '16 17:02

Dan


2 Answers

I believe that I have found a way that seems to work but isn't global like the ServicePointManager.ServerCertificateValidationCallback approach that is typically recommended. I started by making a subclass of the SignalR "DefaultHttpClient" class as follows:

class CustomHttpClient : DefaultHttpClient
    {
        private readonly System.Net.Security.RemoteCertificateValidationCallback _serverCertificateValidationCallback;

        public CustomHttpClient (System.Net.Security.RemoteCertificateValidationCallback serverCertificateValidationCallback) : base()
        {
            this._serverCertificateValidationCallback = serverCertificateValidationCallback;
        }

        protected override HttpMessageHandler CreateHandler()
        {
            var rv = base.CreateHandler() as WebRequestHandler;
            if (this._serverCertificateValidationCallback != null)
                rv.ServerCertificateValidationCallback = this._serverCertificateValidationCallback;
            return rv;
        }
    }

Now I can use my custom HttpClient implementation when I call "Start" on my HubConnection instance as follows:

var hubConnection = new HubConnection("my server url");
var myHub = hubConnection.CreateHubProxy("my hub name");
hubConnection.Start(new CustomHttpClient((sender, certificate, chain, sslPolicyErrors) =>
                {
                    //put some validation logic here if you want to.
                    return true;
                }));

This should allow you to validate the server certificate as you see fit, but keep the scope to the current HubConnection instead of affecting all HTTP traffic from your app.

like image 50
Bryan Bosley Avatar answered Sep 23 '22 07:09

Bryan Bosley


You should register a method for ServerCertificateValidationCallback event.

This code just registers an anonymous method which returns true when the event is fired.

ServicePointManager.ServerCertificateValidationCallback +=
                  (sender, certificate, chain, sslPolicyErrors) => true;

Be careful, this is a global setting. So all ssl/tls request signalr or http will use this setting.

like image 39
Erkan Demirel Avatar answered Sep 23 '22 07:09

Erkan Demirel