Skip to content

[Bug] MSAL.NET Authentication Error in WebView on iOS during AcquireTokenSilent with SharePoint Pages #5253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kezack opened this issue Apr 24, 2025 · 4 comments
Labels
needs attention Delete label after triage public-client untriaged Do not delete. Needed for Automation

Comments

@kezack
Copy link

kezack commented Apr 24, 2025

Library version used

4.71.0

.NET version

.net 8

Scenario

PublicClient - mobile app

Is this a new or an existing app?

The app is in production, I haven't upgraded MSAL, but started seeing this issue

Issue description and reproduction steps

I'm encountering an issue with MSAL.NET and the WebView when using WithUseEmbeddedWebView set to true in a .NET 8 MAUI application on iOS. The initial authentication works fine with WithUseEmbeddedWebView. However, after that, when calling AcquireTokenSilent, I get an authentication error in the WebView when attempting to load SharePoint pages. This issue does not occur on Android.

Steps to Reproduce:

  1. Implement MSAL.NET authentication with WithUseEmbeddedWebView set to true.
  2. Perform the first authentication and acquire a token (this works fine).
  3. On subsequent app launches, call AcquireTokenSilent to silently acquire the token.
  4. Attempt to load SharePoint pages in the WebView after acquiring the token.
  5. Observe that on iOS, an authentication error occurs in the WebView during the AcquireTokenSilent call when loading the SharePoint pages.

Relevant code snippets

public async Task<AuthenticationResult> LoginAsync(CancellationToken cancellationToken)
        {
            AuthenticationResult result;
            try
            {
                var accounts = await authenticationClient.GetAccountsAsync();
                if (accounts.Any())
                {
                    result = await authenticationClient
                        .AcquireTokenSilent(Constants.Scopes, accounts.FirstOrDefault())
                        .ExecuteAsync(cancellationToken);
                }
                else
                {
                    result = await authenticationClient
                    .AcquireTokenInteractive(Constants.Scopes)
#if ANDROID
                   .WithParentActivityOrWindow(Platform.CurrentActivity)
#endif
                    .WithUseEmbeddedWebView(true)
                    .ExecuteAsync(cancellationToken);
                }
                return result;
            }
            catch (MsalClientException ex)
            {
                try
                {
                    Console.WriteLine("LoginAsync : " + ex.Message);
                    result = await authenticationClient
                        .AcquireTokenInteractive(Constants.Scopes)
#if ANDROID
                   .WithParentActivityOrWindow(Platform.CurrentActivity)
#endif
                        .WithUseEmbeddedWebView(true)
                        .ExecuteAsync(cancellationToken);
                    return result;
                }
                catch
                {
                    throw;
                }
            }
            catch (MsalUiRequiredException)
            {
                return await authenticationClient.AcquireTokenInteractive(Constants.Scopes)
#if ANDROID
                   .WithParentActivityOrWindow(Platform.CurrentActivity)
#endif
                   .WithUseEmbeddedWebView(true)
                   .ExecuteAsync(cancellationToken);
            }
        }

Expected behavior

The application should silently acquire the token during AcquireTokenSilent and load SharePoint pages in the WebView without triggering an authentication error on iOS, similar to the behavior on Android.

Identity provider

Microsoft Entra ID (Work and School accounts and Personal Microsoft accounts)

Regression

No response

Solution and workarounds

No response

@kezack kezack added needs attention Delete label after triage untriaged Do not delete. Needed for Automation labels Apr 24, 2025
@Supaibo
Copy link

Supaibo commented Apr 25, 2025

+1

3 similar comments
@nrubio-powellsoftware
Copy link

+1

@orty
Copy link

orty commented Apr 25, 2025

+1

@ottorinobruni
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs attention Delete label after triage public-client untriaged Do not delete. Needed for Automation
Projects
None yet
Development

No branches or pull requests

5 participants