【问题标题】:SignalR The SSL connection could not be establishedSignalR 无法建立 SSL 连接
【发布时间】:2021-02-24 23:32:17
【问题描述】:

我正在使用 SignalR 在我的 ASP.NET 服务器和 Xamarin.Forms 客户端之间进行通信。但是,当我使用 https 时,HubConnection 在尝试 .StartAsync() 时会抛出此异常:System.Net.Http.HttpRequestException: 'The SSL connection could not be established, see inner exception. 但是,当我改用 http 时,一切正常。 请帮我! 提前谢谢!

【问题讨论】:

    标签: c# asp.net xamarin.forms signalr


    【解决方案1】:

    所以,我通过将 Xamarin.Forms 共享项目 .NETStandard 版本升级到 2.1 并以这种方式重写 HubConnectionBuilder 来解决它:

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

    【讨论】:

    • 请记住将您的回复标记为答案,有助于其他人找到此答案,谢谢。
    • 好的,这解决了我的问题。安全呢?此代码是否只接受所有证书?
    • 将 HttpClientHandler 替换为 AndroidClientHandler 会更好。但是如何为 SignalR 做呢?
    【解决方案2】:
     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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多