Igal Flegmann
Igal Flegmann

Reputation: 642

Blazor WASM AAD auth always returns to homepage

I have my Blazor WASM site set up with Azure AD Authentication, and it works great. However, if I am sent to authenticate from any page that is not the homepage (for example mysite.com/counter), when the auth is successful I am redirected to the homepage (mysite.com) I assume there is some state that I can save client side of where the user was before the user was redirected for authentication but I cannot find it.

Edit: I Did some more digging and realized that if a user already has sign in before and is coming back to the site with a page link (for example: mysite.com/counter), it works no problem. However, if a user has not authenticated and it is sent to the login.microsoftonline.com by the authorize attribute of my page the redirect url that is sent is the mysite.com/authentication/login-callback instead of the mysite.com/counter

Upvotes: 0

Views: 616

Answers (1)

Joy Wang
Joy Wang

Reputation: 42163

You could use RedirectToLogin component, it preserves the current URL that the user is attempting to access so that they can be returned to that page if authentication is successful.

@inject NavigationManager Navigation
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@code {
    protected override void OnInitialized()
    {
        Navigation.NavigateTo(
            $"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
    }
}

Reference - https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-azure-active-directory?view=aspnetcore-5.0#redirecttologin-component

Upvotes: 1

Related Questions