Share via

MSAL authentication_canceled exception in .NET MAUI even when using embedded webview

nandu kathmandi 31 Reputation points
2026-02-19T12:45:03.5+00:00

Hi,

I am facing a recurring issue in a .NET MAUI Android application using MSAL 4.77.0 for Azure AD authentication.

I am seeing frequent non-fatal exceptions in Crashlytics:

Microsoft.Identity.Client.MsalClientException ErrorCode: authentication_canceled Message: User canceled authentication. On an Android device, this could be due to the lack of capabilities, such as custom tabs, for the system browser.

Scenario

  • Platform: .NET MAUI (.NET 9)
  • MSAL Version: 4.77.0
  • Broker: disabled (WithBroker(false))
  • We are using embedded webview:
      private Task<AuthenticationResult> AcquireTokenInteractive(string[] scopes)
              {
                  var tcs = new TaskCompletionSource<AuthenticationResult>();
                  MainThread.BeginInvokeOnMainThread(async () =>
                  {
                      try
                      {
                          var authResult = await _pca.AcquireTokenInteractive(scopes)
                                              .WithParentActivityOrWindow(App.RootViewController)
                                              .WithUseEmbeddedWebView(true)
                                              .ExecuteAsync();
                          tcs.TrySetResult(authResult);
                      }
                      catch (Exception ex)
                      {
                          if (Common.MSAuthService != null && Common.DataService != null)
                          {
                              await Common.DataService.LogToAzureStorage(Common.MSAuthService, Common.LogException("AcquireTokenInteractive", ex.Message, ex.StackTrace));
                          }
                          CrashlyticsLogger.LogException("AcquireTokenInteractive", ex);
                          tcs.TrySetException(ex);
      
                      }
                  });
                  return tcs.Task;
              }
      _pca.AcquireTokenInteractive(scopes)
          .WithParentActivityOrWindow(parent)
          .WithUseEmbeddedWebView(true)
          .ExecuteAsync();
      
    

Observations

  1. This happens in production for multiple users.
  2. It appears as a non-fatal exception in Crashlytics.
  3. Many occurrences (e.g., 300+ across ~100 users).
  4. Users are not explicitly reporting login failure.
  5. AuthenticationAgentActivity opens and immediately returns to MainActivity before the exception.

Additional Details

We initially added the following in MainActivity:

 protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
  {
            base.OnActivityResult(requestCode, resultCode, data);

            try
            {
                AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
                if (resultCallbackvalue != null)
                {
                    resultCallbackvalue(requestCode, resultCode, data);
                    resultCallbackvalue = null;
                }
            }
            catch (Exception ex)
            {
                Common.DataService.LogToAzureStorage(Common.MSAuthService, Common.LogException("OnActivityResult", ex.Message, ex.StackTrace));
                CrashlyticsLogger.LogException("OnActivityResult", ex);
            }
 }

Questions

  1. In .NET MAUI, is AuthenticationContinuationHelper required or should it be removed entirely?
  2. When using .WithUseEmbeddedWebView(true), can authentication_canceled still occur due to lifecycle issues?
  3. Is .WithParentActivityOrWindow(Platform.CurrentActivity) recommended in MAUI, or should MSAL auto-detect the activity?
  4. What is the recommended configuration for stable MSAL authentication in .NET MAUI Android?
  5. Is there any known issue with MSAL 4.77.0 on Android 13+ that could trigger this behavior?

I want to understand:

  • if there is a lifecycle/configuration issue in MAUI

And how to prevent unnecessary authentication_canceled exceptions in production logs

Any guidance on best practices for MSAL + .NET MAUI Android authentication would be appreciated.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 14,955 Reputation points Microsoft External Staff Moderator
    2026-02-20T05:04:51.9166667+00:00

    Hi @nandu kathmandi ,

    Thanks for reaching out.

    It looks like the authentication_canceled exceptions you’re seeing are most likely caused by how the Android activity lifecycle interacts with MSAL in .NET MAUI, rather than actual user cancellations. Even with .WithUseEmbeddedWebView(true), MSAL still launches a temporary AuthenticationAgentActivity, and if that activity is destroyed or not correctly tied to a stable parent, MSAL interprets it as the user canceling.

    The users aren't noticing these failures, which is worth investigating carefully. MSAL does not automatically retry silently after an authentication_canceled - a canceled interactive flow leaves nothing new in the token cache, so any immediate silent call would just fail again with user_interaction_required

    The more likely explanation is that AcquireTokenInteractive is being called proactively somewhere in your app - on app resume, during page navigation, or as part of a background token check - rather than only in direct response to a user-initiated sign-in. In that case, the cancellation happens before the user even sees a login screen, so they never notice it, and the next attempt succeeds because the timing or lifecycle state is different by then. 

    I'd audit where AcquireTokenInteractive is being triggered across your app to confirm it's only called from explicit user actions. 

    Just to set expectations, MSAL will report authentication_canceled for any incomplete interactive flow, including: 

    • A fast back-press 
    • The activity being destroyed during a redirect 
    • Timing mismatches in the lifecycle 

    I suggest the following recommendations (you can treat these as a reference while verifying what applies best to your app):

    1. Use the current Android activity as the parent for MSAL calls:
    .WithParentActivityOrWindow(Platform.CurrentActivity)
    

    This is generally safer than passing App.RootViewController and avoids lifecycle mismatches.

    1. Remove AuthenticationContinuationHelper if you’re using embedded webview, as it’s typically not required in modern MAUI apps and can sometimes introduce unnecessary handling, as confirmed in this MSAL GitHub issue

    While this is a non- Microsoft link, it is a official repository maintained by Microsoft. 

    1. Make sure only one interactive authentication call runs at a time and avoid triggering it during page transitions or navigation events.
    2. Optional but helpful: set your MainActivity launch mode to SingleTask to reduce the chance of activity recreation during auth.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.


  2. Q&A Assist
    2026-02-19T12:45:15.0033333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more
    1. AuthenticationContinuationHelper: In .NET MAUI, AuthenticationContinuationHelper is generally used to handle the continuation of authentication after the user returns from the authentication activity. It is recommended to keep it in your OnActivityResult method to ensure that the authentication flow is properly handled. Removing it may lead to issues where the authentication result is not processed correctly.
    2. Embedded WebView and authentication_canceled: Yes, even when using .WithUseEmbeddedWebView(true), the authentication_canceled exception can still occur. This can happen if the user navigates away from the authentication process or if there are lifecycle issues that cause the authentication activity to be interrupted.
    3. Using .WithParentActivityOrWindow(Platform.CurrentActivity): It is generally recommended to specify the parent activity explicitly using .WithParentActivityOrWindow(Platform.CurrentActivity). While MSAL can auto-detect the activity, explicitly setting it can help avoid issues related to activity context, especially in complex applications.
    4. Recommended Configuration: For stable MSAL authentication in .NET MAUI Android, ensure that:
      • You are handling the authentication continuation properly in OnActivityResult.
      • You are using the correct version of MSAL that is compatible with your target framework.
      • You have a fallback mechanism for handling cases where authentication fails or is canceled.
      • Monitor the logs for any specific patterns that might indicate lifecycle issues or user behavior leading to cancellations.
    5. Known Issues with MSAL 4.77.0 on Android 13+: There may be specific issues related to MSAL 4.77.0 on Android 13+, particularly regarding how the embedded web view interacts with the system browser capabilities. It's advisable to check the MSAL GitHub repository or documentation for any reported issues or updates regarding this version and platform compatibility.

    To prevent unnecessary authentication_canceled exceptions in production logs, consider implementing better error handling and user feedback mechanisms. This could include informing users about the need for a compatible browser or providing clear instructions on how to complete the authentication process without interruptions.

    0 comments No comments

Your answer

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