找不到证书路径的信任锚 [ Xamarin.Forms ]

Trust anchor for certification path not found [ Xamarin.Forms ]

我在尝试使用 Open VPN POST 到 https API 时遇到以下异常

Note: When I try accessing the API using swagger in tablet on which Open VPN is connected, the API is connecting and working fine, it's just that the mobile app isn't able to consume it. ( in the Xamarin app we are getting the exception.

代码:

var uri = prepareLoginUri("/Login/Login");
var body = new { username, password };
var json = JsonConvert.SerializeObject(body);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await _client.PostAsync(uri, content);

异常:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

已找到此异常的解决方案但未解决问题:

https://forums.xamarin.com/discussion/91782/trust-anchor-for-certification-path-not-found

根据这个link中的建议,我在MainActivity.cs

中添加了如下代码
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
_client = new HttpClient(clientHandler);

这解决了异常,但现在我从 API 得到的响应是 404 Not found.

StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent

但大摇大摆地,API 工作得非常好。

尽量在 BaseAddress 的末尾放置一个斜杠 (/),并且一定不要在相对 URI 的开头放置一个斜杠 (/)。喜欢 :

client.BaseAddress = new Uri("http://192.168.1.100:33435/ManageSystem/");
var response = await client.GetAsync("Login/Login");

Solution

我们需要一个 SSL 证书,但即使在添加一个之后我们也遇到了这个问题但是 这与移动应用程序无关,问题是 SSL 证书 我们没有正确配置证书,我们必须添加中间件 为 HA 代理生成 .pem 文件的证书。

该网站将我们的重点从移动应用程序转移到服务器。 https://www.digicert.com/help/

上面写的很清楚是服务器的问题,下面是link 这帮助我解决了这个问题。

Trust Anchor not found for Android SSL Connection