Certificate error connecting Android Emulator to API on localhost

Mark Evans 45 Reputation points
2025-03-02T07:44:06.1266667+00:00

I'm building a .NET Maui app using VS2022. I'm testing it using the Android emulator. The app gets its data from an API that is running on my local machine. The API is called from C# code but also from JavaScript in a WebView component. I've been struggling for days with a certificate error. As a first step, I want to at least be able to call the API using the browser without certificate errors (see image below). I've tried generating a certificate and installing it on the emulator. I've also tried using the Mitm proxy. Nothing works :(

User's image

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,982 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 49,126 Reputation points Microsoft External Staff
    2025-03-03T01:24:51.7566667+00:00

    Hello,

    This SSL error is expected. It is explained in detail in the official Maui documentation on using local Https services on mobile devices.

    Attempting to invoke a local secure web service from a .NET MAUI app running in an Android emulator will result in a java.security.cert.CertPathValidatorException being thrown, with a message indicating that the trust anchor for the certification path hasn't been found. Similarly, attempting to invoke a local secure web service from a .NET MAUI app running in an iOS simulator will result in an NSURLErrorDomain error with a message indicating that the certificate for the server is invalid. These errors occur because the local HTTPS development certificate is self-signed, and self-signed certificates aren't trusted by Android or iOS. Therefore, it's necessary to ignore SSL errors when an app consumes a local secure web service.

    For details on how to connect to the local https service using a local device, please refer to the detailed steps in the following document.

    A .NET MAUI app running in the Android emulator or iOS simulator can consume an ASP.NET Core web service that's running locally over HTTPS. The process to enable this is as follows:

    1. Trust the self-signed development certificate on your machine. For more information, see Trust your development certificate.
    2. Specify the address of your local machine. For more information, see Specify the local machine address.
    3. Bypass the local development certificate security check. For more information, see Bypass the certificate security check. Each item will be discussed in turn.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.