maui: Android app does not authenticate, when deployed from play store (using IPublicClientApplication)

Updated after further findings

The request times out , after the authentication page redirects to the app , skip to here : https://github.com/dotnet/maui/issues/15784#issuecomment-1638703216

Initial description

I have followed the online instructions to authenticate my dotnet MAUI app against Azure B2C. This works in iOS, this also works on the Android virtual devices (simulators). It even works, when debugging it to the physical device.

But when I publish & sign the app (in Release mode, obviously), and deploy the release through an internal test in the Google Play store, the authentication does not happen, and I don’t get any exception or continuation of the flow.

The relevant code is pasted below.
The behavior I see:

  1. The authentication window pops up, using the default browser of my android phone (Google Chrome in my case).
  2. When I enter the (correct) credentials, nothing happens anymore (no redirection to the app)
  3. When the user opens the app again, and refreshes, the page still shows the user as Unauthenticated. (in the Blazor WebView)
try
{
    AuthenticationResult authResult = null;
    var tokens = new List<AuthToken> { };
    var loggedOnAccount = await GetUserAsync();
    if (loggedOnAccount != null)
    {
        try
        {
            // Removing code, as this is not reached
            appTracer.Trace($"Logged on Account found: {loggedOnAccount.Username}");
        }
        catch (Exception e)
        {
            appTracer.Trace($"Error : {e.Message}");
            loggedOnAccount = null;
        }
    }

    if (loggedOnAccount == null)
    {
        appTracer.Trace($"No account found , asking sign on");
        var authSettings = await GetAuthSettingsAsync();
#if ANDROID
        appTracer.Trace($"Android, so WithParent should be called");
#endif

        try
        {
            authResult = await authSettings.Client
                .AcquireTokenInteractive(authSettings.Settings.Scopes)
                .WithPrompt(Prompt.ForceLogin)
#if ANDROID
            .WithParentActivityOrWindow(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity)
#endif
                .ExecuteAsync();
        }
        catch (Exception e)
        {
            // This line is never reached
            appTracer.Trace(e.ToString());
            throw;
        }
        // This line is never reached
        appTracer.Trace(
            $"Auth result: {(authResult?.AccessToken)?[5..] ?? "NULL"}");
        tokens.Add(new AuthToken("TokenRetrieval", "Logged on"));
    }


    if (authResult != null)
    {
        if (!string.IsNullOrEmpty(authResult.AccessToken))
        {
            tokens.Add(new AuthToken("AccessToken", authResult.AccessToken));
        }

        claimsPrincipal = authResult.ClaimsPrincipal;
        appTracer.Trace($"Persisting tokens");

        await tokenStorage.PersistTokensAsync(tokens);
        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
    }

    return authResult;
}
catch (Exception e)
{
    appTracer.Trace($"An error occurred during login: {e.Message}");
    throw;
}

When I look at the generated output (from the appTracer.Trace events), this is what happens:

21:38:54.314 - Getting user
21:38:54.446 - Current environment: prd
21:38:54.561 - Account found: NULL
21:38:54.676 - Getting authentication state
21:38:56.383 - Getting user
21:38:56.384 - Current environment: prd
21:38:56.385 - Account found: NULL
21:38:56.385 - No account found , asking sign on
21:38:56.385 - Current environment: prd
**21:38:56.385 - Android, so WithParent should be called**
21:39:21.752 - Getting user
21:39:21.752 - Current environment: prd
21:39:21.753 - Account found: NULL

After the bold line (Android, so WithParent…) there should be either an exception (see appTracer), or a line starting with Auth result.

I successfully see the log on screen, it’s just like the code logic is stopped without exception.

Any idea?

Steps to Reproduce

  1. Publish the above code through dotnet publish -f:net7.0-android -c:Release
  2. Upload it as a new release (xxx-Signed.aab) in the Google Play store
  3. Install the app release on a physical device
  4. Open the app , click Sign in
  5. The pop up opens, enter the credentials
  6. See that nothing happens (on the simulator, it works)

Link to public reproduction project repository

https://github.com/SamVanhoutte/maui-android-auth-repro

Version with bug

7.0.86

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11 and up

Did you find any workaround?

no

Relevant log output

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 24 (10 by maintainers)

Most upvoted comments

Hi @SamVanhoutte. We have added the “s/needs-info” label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.