Krzysztof Krysztofczyk
Krzysztof Krysztofczyk

Reputation: 499

Blazor (server) - Redirection to another page that depends user status and page attribute

Currently, I have such code at MainLayout.razor file. That is redirecting the request to any page to the Register page if a user is not logged. That is working quite well still I have two issues with that.

protected async override Task OnInitializedAsync()
{
    base.OnInitialized();
    var user = (await AuthStat).User;
    if (!user.Identity.IsAuthenticated)
    {
        NavigationManager.NavigateTo($"Identity/Account/Register");
    }
}
  1. User is redirected to Identity/Account/Register even if he/she tries to access the page that has AllowAnonymous attribute.

  2. That is not a problem, still, I'm curious why any request to a page other than Identity/Account/Register is redirected to Identity/Account/Register. All except Identity/Account/Login. That is how it should be still I don't know why that works that way.

Upvotes: 0

Views: 338

Answers (1)

Asif Rahman
Asif Rahman

Reputation: 263

you can use like this

<AuthorizeRouteView RouteData="routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <RedirectToLogin></RedirectToLogin>
            </NotAuthorized>
        </AuthorizeRouteView>

full article is here.

Upvotes: 1

Related Questions