Xamarin Android问题通过HTTPS连接到具有自签名证书的站点:&找不到认证路径的信任锚。 [英] Xamarin Android issue connecting via HTTPS to site with self-signed certificate: "Trust anchor for certification path not found."

查看:22
本文介绍了Xamarin Android问题通过HTTPS连接到具有自签名证书的站点:&找不到认证路径的信任锚。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对具有2个SSL证书的站点进行HTTPS调用:一个自签名证书和一个由第一个证书签名的证书。当我使用HttpClient向站点发送请求时,控制台记录一个不受信任的链,显示两个证书,然后打印由java.security.cert.CertPathValidatorException: Trust anchor for certification path not found引起的长堆栈跟踪。

我已经在我的手机上安装了这两个证书,导航到Chrome站点显示了一个可信连接(在我安装证书之前,它有一个不可信连接警告)。我认为问题在于App拒绝信任自签名证书。我没有访问服务器的权限,因此对其证书没有影响,因此安装由受信任的CA签名的证书是不可行的。


我尝试过的解决方案没有奏效。

ServicePointManager.ServerCertificateValidationCallback似乎未运行。

我尝试对ServicePointManager.ServerCertificateValidationCallback使用我自己的函数,但我给它的委托似乎从未运行过。我的MainActivity.OnCreate方法中有以下代码,但控制台从不记录消息:

System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
  Console.WriteLine($"****************************************************************************************************");

  return true;
};

HttpClientHandler.ServerCertificateCustomValidationCallback引发异常。

我尝试使用HttpClientHandler并将其设置为ServerCertificateCustomValidationCallback,但仅收到消息:

System.NotImplementedException: The method or operation is not implemented. at System.Net.Http.HttpClientHandler.set_ServerCertificateCustomValidationCallback (System.Func`5[T1,T2,T3,T4,TResult] value)

安装代码:

HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
HttpClient client = new HttpClient(handler);

推荐答案

我能够让它在安卓和iOS中运行。

iOS很简单,只需覆盖ServicePointManager.ServerCertificateValidationCallback

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

对于Android,我使用了Bruno Caceiro's answer from a similar question和创建的依赖项服务。

在我的Xamarin Forms项目中,我添加了一个简单界面:

public interface IHTTPClientHandlerCreationService
{
  HttpClientHandler GetInsecureHandler();
}

在我的Xamarin Android项目中,我实现了接口:

[assembly: Dependency(typeof(HTTPClientHandlerCreationService_Android))]
namespace MyApp.Droid
{
  public class HTTPClientHandlerCreationService_Android : CollateralUploader.Services.IHTTPClientHandlerCreationService
  {
    public HttpClientHandler GetInsecureHandler()
    {
      return new IgnoreSSLClientHandler();
    }
  }

  internal class IgnoreSSLClientHandler : AndroidClientHandler
  {
    protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
    {
      return SSLCertificateSocketFactory.GetInsecure(1000, null);
    }

    protected override IHostnameVerifier GetSSLHostnameVerifier(HttpsURLConnection connection)
    {
      return new IgnoreSSLHostnameVerifier();
    }
  }

  internal class IgnoreSSLHostnameVerifier : Java.Lang.Object, IHostnameVerifier
  {
    public bool Verify(string hostname, ISSLSession session)
    {
      return true;
    }
  }
}

共享代码以正确设置HttpClient:

switch (Device.RuntimePlatform)
{
  case Device.Android:
    this.httpClient = new HttpClient(DependencyService.Get<Services.IHTTPClientHandlerCreationService>().GetInsecureHandler());
    break;
  default:
    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
    this.httpClient = new HttpClient(new HttpClientHandler());
    break;
}

这篇关于Xamarin Android问题通过HTTPS连接到具有自签名证书的站点:&amp;找不到认证路径的信任锚。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆