c# - Xamarin 中的 SSL 握手失败

标签 c# ssl xamarin lets-encrypt flurl

我正在尝试访问我的 RESTful API 以从 MySQL 数据库中检索数据。在我的 C# WPF 项目中,一切都已设置并完美运行。但是当在 Xamarin Forms(为 Android 构建)中使用完全相同的代码时,我无法与我的服务器成功进行 SSL 握手。

服务器详情

  • 让我们加密 SSL 证书(绝对有效)
  • Apache BasicAuth (.htaccess)
  • 仅限 HTTPS(重写 HTTP),因此端口 443
  • REST API:用于访问 MariaDB 10.3 的 php-crud-api(由 mevdschee 提供)

我正在使用 Flurl.Http(使用 HttpClient)建立连接,但在 jsonReader.Wait() 上出现异常:

var jsonReader = "https://example.com/my_api/api.php/records/my_table?order=id,desc&size=10"
            .WithHeader("Accept", "application/json")
            .WithBasicAuth("username", "password")
            .GetJsonAsync<JsonRootObject>();
            // Wait for completion.
            jsonReader.Wait();


这是我的 AggregateException:

System.AggregateException: One or more errors occurred. (Call failed. An error occurred while sending the request
GET https://example.com/my_api/api.php/records/my_table?order=id,desc&size=10) ---> Flurl.Http.FlurlHttpException: Call failed. An error occurred while sending the request
GET https://example.com/my_api/api.php/records/my_table?order=id,desc&size=10 ---> System.Net.Http.HttpRequestException: An error occurred while sending the request
---> System.Net.WebException: Error: TrustFailure (Authentication failed, 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
at /Users/builder/jenkins/workspace/xamarin-android-d16-1/xamarin-android/external/mono/external/boringssl/ssl/handshake_client.c:1132
at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00038] in <74989b9daab94528ac2c4b7da235b9c5>:0 
at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) [0x000a1] in <74989b9daab94528ac2c4b7da235b9c5>:0 
at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool)
at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00006] in <74989b9daab94528ac2c4b7da235b9c5>:0
at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) [0x000ff] in <74989b9daab94528ac2c4b7da235b9c5>:0
at Mono.Net.Security.AsyncProtocolRequest.StartOperation (System.Threading.CancellationToken cancellationToken) [0x0008b] in <74989b9daab94528ac2c4b7da235b9c5>:0 


我尝试过的/我知道的

  • 代码在 WPF 中工作完美
  • 完全相同的代码(复制粘贴)在 Xamarin Forms 中工作(在 Android Pie 上测试)
  • 在模拟器或专用设备中均无效
  • 通过浏览器访问 REST API有效并按预期提供结果(在我的 PC、Chrome 中的模拟器和我专用的 Android 上测试)
  • 在项目属性 -> Android 选项 -> 高级下更改 HttpClient 实现或 SSL/TLS 实现没有帮助

为什么我的 SSL 握手失败? Xamarin 与 WPF 有何不同?

最佳答案

原来是一个Apache configuration error .我 checked my domain并注意到一个错误。这是通过在我的 Apache 指令中添加证书的链文件来解决的:

SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

最后一行是缺失的那一行。奇怪的是浏览器和 WPF 即使没有链文件也信任证书。无论如何,现在它也适用于 Android。

与此同时,我一直在使用这段代码在我的应用中禁用 SSL 验证:

// This disables the SSL certificate validity check.
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

该代码不安全,只能用于测试。

关于c# - Xamarin 中的 SSL 握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56639354/

相关文章:

ios - xamarin iOS : How to show the audio amplitude of the voice when recording

datetime - 如何在 Xamarin 应用程序中的 XAML 中格式化日期和时间

c# - Xamarin.iOS 应用程序在事件时不接收 parse.com PUSH

c# - 为什么 HttpPlatformHandler 不创建日志文件?

c# - 我可以在触摸屏上渲染带有按钮的 WPF 窗口并读取输入吗?建议?

c# - 以编程方式在 Excel 中插入单元格注释

c# - LINQ 数据表返回 0 而不是空合并

java - android中的客户端证书

ssl - Mule cxf :outbound-endpoint (client) and HTTPS/SSL 问题

c++ thrift 客户端不使用 ssl(SSL_connect 挂起)