Unable to Authenticate MAUI Application on Android

Onyango, David 31 Reputation points
2025-06-14T21:29:52.3133333+00:00

I have a Blazor hybrid app that successfully authenticates against Azure client OpenID authentication scheme on windows but fails on android device. Is there a working example to reference specifically for Android?

Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Onyango, David 31 Reputation points
    2025-06-15T21:35:29.96+00:00

    After several attempts from multiple sources, the link below gives the closest guidance so far but not a working one for Android from my practical experience. https://www.syncfusion.com/blogs/post/authenticate-the-net-maui-app-with-azure-ad. Microsoft needs to provide a working sample.

    Github copilot came in hand to provide both silent and interactive login options which work. See snippet below.

    try

            {
    
                var accounts = await _pca.GetAccountsAsync();
    
                var result = await _pca.AcquireTokenSilent(_scopes, accounts.FirstOrDefault())
    
                    .ExecuteAsync();
    
                AuthResult = result;                
    
                return result;
    
            }
    
            catch (MsalUiRequiredException)
    
            {
    

    #if ANDROID

                var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;
    
                var result = await _pca.AcquireTokenInteractive(_scopes)
    
                    .WithParentActivityOrWindow(activity)
    
                    .ExecuteAsync();
    

    #elif WINDOWS

                var windowHandle = ((MauiWinUIWindow)Microsoft.Maui.Controls.Application.Current.Windows[0].Handler.PlatformView).WindowHandle;
    
                var result = await _pca.AcquireTokenInteractive(_scopes)
    
                    .WithParentActivityOrWindow(windowHandle)
    
                    .ExecuteAsync();
    

    #else

                var result = await _pca.AcquireTokenInteractive(_scopes)
    
                    .ExecuteAsync();
    

    #endif

    return result;

    }

    0 comments No comments

  2. Harry Vo (WICLOUD CORPORATION) 1,315 Reputation points Microsoft External Staff
    2025-08-25T04:21:03.44+00:00

    Hi @Onyango, David

    Is there a working example to reference specifically for Android?

    You can read our official document here: https://learn.microsoft.com/en-us/samples/azure-samples/ms-identity-ciam-dotnet-tutorial/ms-identity-ciam-dotnet-tutorial-2-sign-in-maui/

    From that, you can learn how to use MSAL.NET to authenticate users in a .NET MAUI app targeting Android.

    I hope this helps you get things back on track quickly! If my suggestions can solve your issue, feel free to interact with the system accordingly!

    Thank you!


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.