Xamarin Android问题通过HTTPS连接到具有自签名证书的站点:&找不到认证路径的信任锚。

Xamarin Android issue connecting via HTTPS to site with self-signed certificate: amp;quot;Trust anchor for certification path not found.amp;quot;(Xamarin Android问题通过HTTPS连接到具有自签名证书的站点:amp;找不到认证路径的信任锚。)
本文介绍了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;找不到认证路径的信任锚。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)