SignalR The SSL connection could not be established

Viewed 2547

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!

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();
 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();
Related