Skip to content

[Blazor WASM / MSAL] Race in AuthenticationService.init drops the login return URL (regression in 10.0.8) #69734

Closed as duplicate

Description

@droyad

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Since Microsoft.Authentication.WebAssembly.Msal 10.0.8, a successful redirect login can send the user to the app root (Navigation.BaseUri) instead of the page they asked for.

This regressed in #66094, which added an await to AuthenticationService.init before _initialized is set:

public static async init(settings: AuthorizeServiceConfiguration, jsLoggingOptions: JavaScriptLoggingOptions) {
    if (!AuthenticationService._initialized) {
        AuthenticationService.instance = new MsalAuthorizeService(settings, new Logger(jsLoggingOptions));
        await AuthenticationService.instance.initialize();   // <-- added in #66094
        AuthenticationService.instance.initializeMsalHandler();
        AuthenticationService._initialized = true;
    }
}

Before 10.0.8 this method ran synchronously (despite being marked async), so it couldn't be re-entered. Now, if two .NET callers reach RemoteAuthenticationService.EnsureAuthService() at the same time, both pass the _initialized check. Each one creates its own MsalAuthorizeService and PublicClientApplication, and each calls initializeMsalHandler(), which runs handleRedirectPromise().

A common way to get two concurrent callers on startup:

  • AuthorizeRouteView / CascadingAuthenticationState resolving the auth state (getUser)
  • a component in the layout (for example a nav menu) making an API call through BaseAddressAuthorizationMessageHandler (getAccessToken)

On the login-callback page, both instances run handleResult(result):

  1. The first instance calls retrieveState(null, result.state). That finds the saved { returnUrl } in sessionStorage and removes it.
  2. The second instance gets a result too, but its retrieveState finds nothing, so it returns success(undefined).
  3. The second instance is the one left in AuthenticationService.instance, so completeSignIn() returns { status: "success" } with no state.
  4. RemoteAuthenticatorViewCore.ProcessLogInCallback calls GetReturnUrl(null). There's no history entry state after the full-page redirect, so it falls back to Navigation.BaseUri.

Trace from instrumenting sessionStorage and history (URLs shortened):

/some/page                     | pushState /authentication/login  {"returnUrl":"https://app/some/page",...}
/authentication/login          | sessionStorage.set   ...AuthorizeService.<id> = {"returnUrl":"https://app/some/page"}
/authentication/login-callback | sessionStorage.get   ...AuthorizeService.<id> -> {"returnUrl":"https://app/some/page"}
/authentication/login-callback | sessionStorage.remove ...AuthorizeService.<id>   (retrieveState < handleResult < completeAuthentication)
/authentication/login-callback | sessionStorage.get   ...AuthorizeService.<id> -> null   <-- second instance
/authentication/login-callback | replaceState https://app/  {"userState":null}

After the redirect, await AuthenticationService.instance._redirectCallback gives {"status":"success"} with no state property.

Expected Behavior

After signing in, the user goes back to the ReturnUrl passed to NavigateToLogin, as in 10.0.7 and earlier. AuthenticationService.init should be safe to call concurrently. For example, cache the init promise:

private static _initPromise: Promise<void> | undefined;

public static init(settings: AuthorizeServiceConfiguration, jsLoggingOptions: JavaScriptLoggingOptions) {
    return AuthenticationService._initPromise ??= (async () => {
        const instance = new MsalAuthorizeService(settings, new Logger(jsLoggingOptions));
        await instance.initialize();
        instance.initializeMsalHandler();
        AuthenticationService.instance = instance;
        AuthenticationService._initialized = true;
    })();
}

The .NET side could also serialise EnsureAuthService() (for example with a cached Task), so it doesn't call init more than once.

Steps To Reproduce

  1. Create a Blazor WebAssembly app with AddMsalAuthentication (Entra ID). Use LoginMode = "redirect" and the standard RedirectToLogin component, which calls Navigation.NavigateToLogin(...).
  2. In MainLayout (rendered while auth is still resolving), add a component that calls the API on OnInitializedAsync through an HttpClient that uses BaseAddressAuthorizationMessageHandler. Catch AccessTokenNotAvailableException.
  3. Put an [Authorize] page at /protected.
  4. While signed out, browse straight to /protected and complete the Microsoft sign-in.
  5. Actual: you land on /. Expected: you land on /protected.

Remove the layout component's startup API call and the return URL works again.

Workaround

In Program.cs, resolve the auth state once before RunAsync(), so the JS service is fully initialised before anything can race into it:

var host = builder.Build();
await using (var scope = host.Services.CreateAsyncScope())
    await scope.ServiceProvider.GetRequiredService<AuthenticationStateProvider>().GetAuthenticationStateAsync();
await host.RunAsync();

Exceptions (if any)

None. The navigation just goes to the wrong place.

.NET Version

10.0.12 (regression introduced in 10.0.8; 10.0.7 not affected). The bundled @azure/msal-browser is 4.30.0.

Anything else?

  • I compared AuthenticationService.js in the NuGet packages: 10.0.7 has the synchronous init, and 10.0.8 through 10.0.12 have the await.
  • Browser: Chrome. Hosted Blazor WASM (ASP.NET Core host, MapFallbackToFile("index.html")).
  • main may have the same init shape. Worth checking whether the fix needs to go there as well as release/10.0.

Activity

  1. MihuBot commented on Oct 9, 2026

    @MihuBot
  2. github-actions commented on Oct 9, 2026

    @github-actions
    Contributor

    Triage Summary

    Area: area-auth (MSAL login/redirect handling in Microsoft.Authentication.WebAssembly.Msal)
    Type: Bug (login return URL is dropped after redirect sign-in)

    Regression Info

    • Previously working version: 10.0.7
    • Failing version: 10.0.8 through 10.0.12 (as reported)
    • After sign-in, the user lands on Navigation.BaseUri instead of the requested ReturnUrl. The reporter compared AuthenticationService.js across packages and attributes the change to the await added in AuthenticationService.init by [release/10.0] Update azure/msal-browser #66094.

    Potential Duplicates

    Notes

    Generated by Issue Triage Agent for dotnet/aspnetcore for #69734 · copilot · auto · 29.6 AIC · ⌖ 1 AIC · ⊞ 30.4K · ◷

  3. droyad commented on Oct 9, 2026

    @droyad
    Author

    Closing as a duplicate of #68136. I've moved the root cause, trace and workaround there, and linked the fixes: #69735 and #69736.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions