简体   繁体   English

ClientWebsocket SSL 证书验证失败

[英]ClientWebsocket SSL Certificate Verification Failed

I am writing an app in Xamarin.Forms targeting the .Net Standard 2.1, primarily focused on Android builds.我正在用 Xamarin.Forms 编写一个针对 .Net Standard 2.1 的应用程序,主要侧重于 Android 构建。

I have some websocket client code to connect to my secure self-signed server;我有一些 websocket 客户端代码可以连接到我的安全自签名服务器; however, it won't verify the certificate.但是,它不会验证证书。 I have found some references to use the ServicePointManager for validation callback or the ClientWebSocketOptions.RemoteCertificateValidationCallback but neither callback gets called while connecting.我发现了一些使用 ServicePointManager 进行验证回调或ClientWebSocketOptions.RemoteCertificateValidationCallback但在连接时没有调用回调。 Here's the connect code:这是连接代码:

public async Task Connect(string url) {
            if (client != null) {
                if (client.State == WebSocketState.Open) return;
                else client.Dispose();
            }
            ServicePointManager.ServerCertificateValidationCallback = ValidateServerCert;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            client = new ClientWebSocket();
            client.Options.AddSubProtocol("json");
            client.Options.UseDefaultCredentials = false;
            client.Options.RemoteCertificateValidationCallback = ValidateServerCert;
            if (CTS != null) CTS.Dispose();
            CTS = new CancellationTokenSource();
            Console.WriteLine("TBL: Connecting to " + url + "...");
            try {
                await client.ConnectAsync(new Uri(url), CTS.Token);
            } catch (Exception e) {
                Console.WriteLine("TBL: Ex: " + e);
            }
            Console.WriteLine("TBL: Client state: " + client.State);
            if (client.State == WebSocketState.Open) {
                OnConnect?.Invoke(); // if no callbacks registered, it's null
                Console.WriteLine("TBL: Websocket connected");
                await Task.Factory.StartNew(ReceiveLoop, CTS.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }
        }

With this printed in the Device Log (I can't embed pictures yet):将此打印在设备日志中(我还不能嵌入图片):

Device Log Output设备日志输出

I've scoured the interwebs for answers/hints at this and most seem to apply to HTTPS and not Websockets (or at least their solutions don't seem to work).我已经在互联网上搜索了答案/提示,大多数似乎适用于 HTTPS 而不是 Websockets(或者至少他们的解决方案似乎不起作用)。 I can't get the validation callback to be called, no matter how I arrange this.无论我如何安排,我都无法调用验证回调。 Any help is greatly appreciated.任何帮助是极大的赞赏。

https://stackoverflow.com/a/26336847/9516 https://stackoverflow.com/a/26336847/9516

This accepts all certificates, even self signed ones: ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };这接受所有证书,甚至是自签名证书: ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM