Skip to content

Blazor Wasm MSAL authentication stuck on logging in when msal.js returns "Operation Completed" #43317

Closed
@njannink

Description

@njannink

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In rare cases especially after an update of our Blazor Wasm website with MSAL (Azure B2C) stays stuck on logging-in. It's difficult to reproduce, but I saw it happening on a iPhone with Chrome. What I saw is that the url only consisted of https://xxx.com/authentication/login-callback resulting in the app being stuck on infinite 'logging-in', because with that url the masl.js libarary only returns operation completed which is not handled in the Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore<TState>.ProcessLogInCallback.

I created the following fix by overriding the 'RemoteAuthenticatorView', but would like to get your opinion about the possible risks\implications of this and maybe this will need to be fixed in the original 'RemoteAuthenticationView' aswell.

public sealed class AuthenticatorViewEx : RemoteAuthenticatorView
{
    [Inject] internal NavigationManager Navigation { get; set; }
    [Inject] internal AuthenticationStateProvider AuthenticationStateProvider { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();

        // Fix stuck in login-callback => redirect to home or login
        if (Action == RemoteAuthenticationActions.LogInCallback && Navigation.Uri.Contains($"authentication/{RemoteAuthenticationActions.LogInCallback}", StringComparison.OrdinalIgnoreCase))
        {
            var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();

            if (state.User.Identity is { IsAuthenticated: true })
            {
                Navigation.NavigateTo(Navigation.BaseUri, false, true);
            }
            else
            {
                Navigation.NavigateTo($@"authentication/{RemoteAuthenticationActions.LogIn}");
            }
        }
    }
}

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0(.8)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.area-blazorIncludes: Blazor, Razor Componentsfeature-blazor-msalThis issue is related to MSAL usage in Blazorfeature-blazor-wasmThis issue is related to and / or impacts Blazor WebAssembly

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions