I'm trying to add a custom authentication for my Blazor Server app and I can't get it to redirect an unauthorized user to the login page.
I've already tried this:
This is my app.razor component
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>No encontrado</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Dirección inválida.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
This is my RedirectToLogin component
@inject NavigationManager NavManager
@code {
protected override async Task OnInitializedAsync()
{
NavManager.NavigateTo("/login", forceLoad: true);
}
}
But it seems the <NotAuthorized> section is never reached as I set a breakpoint on the OnInitializedAsync method and the debugging never stopped.
I'm using cookies for authentication and it works as I added an <AuthorizeView> tag in my MainLayout to test it showing a text for not authenticated users.