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?

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

1 answer

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

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.