Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Android emulator cannot trust your local ASP.NET Core HTTPS API

ErrorTrust anchor for certification path not found.

Short answer

The local ASP.NET Core HTTPS certificate is self-signed, and Android does not trust it. In a debug build, give HttpClient a handler that accepts the localhost certificate, call the API through 10.0.2.2, and trust the dev certificate on your machine. Or use plain HTTP with cleartext allowed for the debug build.

  • Applies to .NET MAUI 9, ASP.NET Core 9, Android emulator
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which address and scheme the app calls. An https URL to 10.0.2.2 or localhost means the self-signed certificate is the problem; anything else is a different error.

    dotnet dev-certs https --check --trust
    grep -rn "10.0.2.2\|localhost" --include=*.cs .
  2. 2Match what you see, then jump to its fix

    1. 1CertPathValidatorException with the trust anchor message when the app calls https://10.0.2.2:<port>The HttpClient does not accept the self-signed development certificateFix
    2. 2The request goes to https://localhost:<port> from the Android emulatorThe app calls localhost, which is the emulator itselfFix
    3. 3The dev-certs check reports that no trusted HTTPS development certificate was foundThe development certificate is not trusted on your computerFix
    4. 4Switching the debug URL to http://10.0.2.2:<port> fails because Android blocks cleartext trafficYou only need HTTP for local developmentFix

What does the error look like?

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

Cause 1 of 4

The HttpClient does not accept the self-signed development certificate

1Check

Look at how the HttpClient is created. If it is built with the default handler, Android validates the server certificate against its own store, which does not contain the ASP.NET Core development certificate.

grep -rn "new HttpClient\|HttpClientHandler" --include=*.cs .

2Fix

Create the client with an HttpClientHandler whose ServerCertificateCustomValidationCallback accepts the localhost certificate, and compile it only into debug builds so production keeps normal validation.

var handler = new HttpClientHandler();

#if DEBUG
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
    if (cert != null && cert.Issuer.Equals("CN=localhost"))
        return true;
    return errors == System.Net.Security.SslPolicyErrors.None;
};
#endif

var client = new HttpClient(handler);

3Verify

Run the app in the emulator again and repeat the request.

dotnet build -f net9.0-android -c Debug

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 4

The app calls localhost, which is the emulator itself

1Check

Inside the Android emulator, 10.0.2.2 is the alias of your computer's loopback address. The iOS simulator shares the host network, so localhost works there only.

grep -rn "localhost" --include=*.cs .

2Fix

Pick the address by platform with DeviceInfo.

public static string BaseAddress =
    DeviceInfo.Platform == DevicePlatform.Android ? "https://10.0.2.2:5001" : "https://localhost:5001";

3Verify

Open the same URL from the app and see a response instead of a certificate or connection error.

curl -k https://localhost:5001/api/todoitems/

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

The development certificate is not trusted on your computer

1Check

Installing the .NET SDK installs the ASP.NET Core HTTPS development certificate, but it is not trusted until you trust it once.

dotnet dev-certs https --check --trust

2Fix

Trust the certificate with the dev-certs tool. This is a one-time step.

dotnet dev-certs https --trust

3Verify

Open the API in a desktop browser without a warning, then retest in the emulator.

dotnet dev-certs https --check --trust

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

You only need HTTP for local development

1Check

Apps targeting Android 9 (API 28) or higher block cleartext HTTP unless the app opts in. Check whether the Application attribute sets UsesCleartextTraffic.

grep -rn "UsesCleartextTraffic" Platforms/Android/

2Fix

Run the API with an http profile, call http://10.0.2.2:<port>, and allow cleartext for debug builds only.

#if DEBUG
[Application(UsesCleartextTraffic = true)]
#else
[Application]
#endif
public class MainApplication : MauiApplication
{
    public MainApplication(IntPtr handle, JniHandleOwnership ownership)
        : base(handle, ownership) { }

    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}

3Verify

Start the API with the http launch profile and call it from the emulator.

dotnet run --launch-profile http

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep the certificate bypass inside #if DEBUG so it can never ship in a release build.
  • Keep the base address in one place and choose it by platform.
  • Use a certificate from a real certificate authority for any staging or production API.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android app crashes because the Apache HTTP classes are missing

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;

From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.