Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR The SSL connection could not be established

I'm using SignalR to communicate between my ASP.NET server and Xamarin.Forms client. However, when I'm using https, HubConnection throws this exception when trying to .StartAsync() : System.Net.Http.HttpRequestException: 'The SSL connection could not be established, see inner exception. However, when I'm using http instead, all works fine. Please, help me! Thank you in advance!

like image 409
Fedir Katushonok Avatar asked May 20 '20 05:05

Fedir Katushonok


Video Answer


2 Answers

So, I solved it by upgrading Xamarin.Forms shared project .NETStandard version to 2.1, and rewriting HubConnectionBuilder this way:

hubConnection = new HubConnectionBuilder().WithUrl(Properties.Resources.ServerIPAddress + "/test",
                options =>
                {
                    options.WebSocketConfiguration = conf =>
                    {
                        conf.RemoteCertificateValidationCallback = (message, cert, chain, errors) => { return true; };
                    };
                    options.HttpMessageHandlerFactory = factory => new HttpClientHandler
                    {
                        ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
                    };
                    options.AccessTokenProvider = () => Task.FromResult(Token);
                }).Build();
like image 160
Fedir Katushonok Avatar answered Oct 13 '22 00:10

Fedir Katushonok


 hubConnection = new HubConnectionBuilder()
     .WithUrl($"your host/chatHub", (opts) =>
     {
         opts.HttpMessageHandlerFactory = (message) =>
         {
             if (message is HttpClientHandler clientHandler)
                 // bypass SSL certificate
                 clientHandler.ServerCertificateCustomValidationCallback +=
                     (sender, certificate, chain, sslPolicyErrors) => { return true; };
             return message;
         };
     }).Build();
like image 40
Luis Daniel martinez Avatar answered Oct 13 '22 00:10

Luis Daniel martinez