javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
I tried to connect with middleware to integrating Web API and MAUI and I got Reference issue and Runtime package missing issue.
I'm expecting that the Web API to connect with MAUI android emulator
Place the below file (network_security_config.xml) within the Resources/xml directory of your Android project.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system"/>
</trust-anchors>
</base-config>
</network-security-config>
Cleartext Traffic Permitted : By setting cleartextTrafficPermitted="true", your app can make unencrypted HTTP requests. While useful for development, it's recommended to switch to HTTPS for production to ensure data security.
Trust Anchors : The section with means your app trusts the system's pre-installed CA certificates. This doesn't directly address trusting self-signed certificates but ensures that certificates from recognized CAs are trusted.
To use this configuration in a .NET MAUI Android application, you need to reference it in your Android manifest file (AndroidManifest.xml) like so:
<application android:networkSecurityConfig="@xml/network_security_config">
...
</application>
Implementing IHttpHelper Interface in C#
The IHttpHelper interface and its implementation HttpHelper allow you to customize how your application handles SSL certificate validation.
public interface IHttpHelper
{
HttpClientHandler GetInsecureHandler();
}
public class HttpHelper : IHttpHelper
{
public HttpClientHandler GetInsecureHandler()
{
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert.Issuer.Equals("CN=localhost"))
return true;
return errors == System.Net.Security.SslPolicyErrors.None;
};
return handler;
}
}
Usage
var httpHelper = new HttpHelper();
var handler = httpHelper.GetInsecureHandler();
var httpClient = new HttpClient(handler);
Also note that :
The correct address to use when trying to access your local development server from an Android emulator is 10.0.2.2.