SignalR 与 Xamarin 客户端在调用 StartAsync() 方法时获得“CERTIFICATE_VERIFY_FAILED”
Posted
技术标签:
【中文标题】SignalR 与 Xamarin 客户端在调用 StartAsync() 方法时获得“CERTIFICATE_VERIFY_FAILED”【英文标题】:SignalR with Xamarin client getting "CERTIFICATE_VERIFY_FAILED" on calling StartAsync() method 【发布时间】:2020-07-02 19:24:38 【问题描述】:从我的 Xamarin 客户端调用 SignalR StartAsync 让我很吃惊:
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Mono.Btls.MonoBtlsException: Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
谁能帮我解决这个 CERTIFICATE_VERIFY_FAILED 问题??
这是堆栈跟踪:
[错误] 致命的未处理异常:System.Net.Http.HttpRequestException:无法建立 SSL 连接,请参阅内部异常。 ---> System.Security.Authentication.AuthenticationException:身份验证失败,请参阅内部异常。 ---> Mono.Btls.MonoBtlsException:Ssl 错误:1000007d:SSL 例程:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED 03-21 23:59:57.526 E/mono-rt (6307): 在/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/boringssl/ssl/handshake_client.c:1132 03-21 23:59:57.526 E/mono-rt (6307): 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/ 中的 Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00042]发布/mcs/class/System/Mono.Btls/MonoBtlsContext.cs:220 03-21 23:59:57.526 E/mono-rt (6307): 在 Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) [0x000da] in /Users/builder /jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:840 03-21 23:59:57.526 E/mono-rt (6307): at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool) 03-21 23:59:57.526 E/mono-rt (6307): 在 Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus 状态) [0x00000] 在 /Users/builder/jenkins/workspace/ archive-mono/2019-08/android/release/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:289 03-21 23:59:57.526 E/mono-rt (6307): 在 Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancelToken) [0x000fc] 在 /Users/builder/jenkins/workspace/archive- mono/2019-08/android/release/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:223 03-21 23:59:57.526 E/mono-rt(6307):---内部异常堆栈跟踪结束--- 03-21 23:59:57.526 E/mono-rt (6307): 在 Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Boolean runSynchronously, Mono.Net.Security.MonoSslAuthenticationOptions options, System.Threading.CancellationToken cancelToken) [ 0x0025c] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:406 03-21 23:59:57.526 E/mono-rt (6307): 在 System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore (System.IO.Stream 流, System.Net.Security.SslClientAuthenticationOptions sslOptions, System.Threading.CancellationToken cancelToken ) [0x0007b] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper。 CS:165 03-21 23:59:57.526 E/mono-rt(6307):---内部异常堆栈跟踪结束--- 03-21 23:59:57.526 E/mono-rt (6307): 在 System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore (System.IO.Stream 流, System.Net.Security.SslClientAuthenticationOptions sslOptions, System.Threading.CancellationToken cancelToken ) [0x000f6] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper。 CS:176 03-21 23:59:57.526 E/mono-rt (6307): 在 System.Threading.Tasks.ValueTask
1[TResult].get_Result () [0x0001b] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813 03-21 23:59:57.526 E/mono-rt ( 6307): at System.Net.Http.HttpConnectionPool.CreateConnectionAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x002d8] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:394 03-21 23:59:57.526 E/mono-rt ( 6307): at System.Threading.Tasks.ValueTask
1[TResult].get_Result () [0x0001b] 在 /Users/builder/jenkins/workspace/archive -mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813 03-21 23:59:57.526 E/mono-rt (6307): 在 System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync (System.Threading.Tasks.ValueTask`1[TResult] creationTask) [0x000a2] 在 /Users/builder /jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:529
这里是异常详情:
System.Net.Http.HttpRequestException Message=无法建立 SSL 连接,请参阅内部异常。 源=mscorlib 堆栈跟踪: 在 System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(System.IO.Stream 流,System.Net.Security.SslClientAuthenticationOptions sslOptions,System.Threading.CancellationToken cancelToken)[0x000f6] 在 /Users/builder/jenkins/workspace/archive-mono /2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs:176 在 System.Threading.Tasks.ValueTask
1[TResult].get_Result () [0x0001b] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813 at System.Net.Http.HttpConnectionPool.CreateConnectionAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x002d8] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:394 at System.Threading.Tasks.ValueTask
1[TResult].get_Result () [0x0001b] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/ Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813 在 System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync (System.Threading.Tasks.ValueTask1[TResult] creationTask) [0x000a2] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:529 at System.Threading.Tasks.ValueTask
1[TResult].get_Result () [0x0001b] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/ android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813 在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync(System.Net.Http.HttpRequestMessage 请求,System.Boolean doRequestAuth,System.Threading.CancellationToken cancelToken)[0x0003f] 在 /Users/builder/jenkins/workspace/archive-mono/2019 -08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:284 在 System.Net.Http.RedirectHandler.SendAsync(System.Net.Http.HttpRequestMessage 请求,System.Threading.CancellationToken cancelToken)[0x00070] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/发布/外部/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs:32 在 Microsoft.AspNetCore.Http.Connections.Client.Internal.AccessTokenHttpMessageHandler.SendAsync (System.Net.Http.HttpRequestMessage 请求,System.Threading.CancellationToken cancelToken) [0x000ff] 在 :0 在 Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler.SendAsync (System.Net.Http.HttpRequestMessage 请求,System.Threading.CancellationToken cancelToken) [0x00095] 在 :0 在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x000b3] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:531 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync (System.Uri url, System.Net.Http.HttpClient httpClient, Microsoft.Extensions.Logging.ILogger logger, System.Threading.CancellationToken cancellationToken) [0x00257] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x00080] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport (Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken) [0x00180] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore (Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken) [0x0011e] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at System.Threading.Tasks.ForceAsyncAwaiter.GetResult () [0x0000c] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync (Microsoft.AspNetCore.Connections.TransferFormat transferFormat, System.Threading.CancellationToken cancellationToken) [0x00091] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync (System.Net.EndPoint endPoint, System.Threading.CancellationToken cancellationToken) [0x00114] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync (System.Net.EndPoint endPoint, System.Threading.CancellationToken cancellationToken) [0x001bf] in <143c191c2fa54eed93ae7f23c7781c1f>:0 at System.Threading.Tasks.ValueTask
1[TResult].get_Result () [0x0001b] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/ android/release/external/corefx/src/Common/src/CoreLib/System/Threading/Tasks/ValueTask.cs:813 在 Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore (System.Threading.CancellationToken cancelToken) [0x000a5] 在:0 在 Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner (System.Threading.CancellationToken cancelToken) [0x0019e] in :0 在 System.Threading.Tasks.ForceAsyncAwaiter.GetResult () [0x0000c] in :0 在 Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync (System.Threading.CancellationToken cancelToken) [0x00091] 在:0 在 C:\Users\sipfe\source\repos\HostelProj v2 - RealTime\MobileApp\MobileApp\Services\HubService.cs:38 中的 MobileApp.Services.HubService.SendMessage(MobileApp.Models.Message 消息)[0x00042] 在 C:\Users\sipfe\source\repos\HostelProj v2 - RealTime\MobileApp\MobileApp\ViewModels\ConversationDetailViewModel.cs:83 中的 MobileApp.ViewModels.ConversationDetailViewModel.ExecuteSendCommand () [0x00155] 在 MobileApp.ViewModels.ConversationDetailViewModel.<.ctor>b__34_0 () [0x00028] 在 C:\Users\sipfe\source\repos\HostelProj v2 - RealTime\MobileApp\MobileApp\ViewModels\ConversationDetailViewModel.cs:55 在 System.Runtime.CompilerServices.AsyncMethodBuilderCore+c.b__7_0(System.Object 状态)[0x00000] 在 /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/referencesource/ mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 在 Android.App.SyncContext+c__DisplayClass2_0.b__0 () [0x00000] in :0 在 Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0 在 Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in :0 在(包装器动态方法)Android.Runtime.DynamicMethodNameCounter.70(intptr,intptr)
谢谢大家
【问题讨论】:
“无法建立 SSL 连接” - 您的服务器是否设置了 SSL 证书? 你好@Jason 我的服务器我刚刚安装了signalR nugget 包,添加到启动services.SignalR 和Route,并添加了一个集线器类,就像他们在教程中显示的那样。所以回答你的问题我认为我没有 ssl 证书,但我不确定。你能告诉我在哪里可以查看我是否有一个吗?一开始我必须添加一个自签名证书以便从浏览器调用我的服务器,但这是一个不同的证书,对吗?问候。 【参考方案1】:通过堆栈溢出查找,解决方法如下:
Ignore SSL errors with signalR Core Client
刚刚向我的 HubConnectionBuilder 添加了选项,它就起作用了!!
HubConnection = new HubConnectionBuilder()
.WithUrl($"https://10.0.2.2:5001/chatHub", (opts) =>
opts.HttpMessageHandlerFactory = (message) =>
if (message is HttpClientHandler clientHandler)
// bypass SSL certificate
clientHandler.ServerCertificateCustomValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => return true; ;
return message;
;
)
.Build();
【讨论】:
【参考方案2】:使用AndroidClientHandler
(Android 的 SSL 库)而不是 HttpClientHandler
。
【讨论】:
ios呢?以上是关于SignalR 与 Xamarin 客户端在调用 StartAsync() 方法时获得“CERTIFICATE_VERIFY_FAILED”的主要内容,如果未能解决你的问题,请参考以下文章
SignalR HubConnectionState = 正在连接
SignalR 集线器连接在 Xamarin Forms iOS 中不起作用