I’m trying to implement authorization in a .NET 8 Blazor server-side app. When a user isn’t logged in and tries to access a page that requires authorization, I want to redirect to the login page. I did the same thing the default Blazor Web App template with “Individual Accounts” does but used a different URI. But my URI is ignored and the redirect goes to /Account/Login. Am I misinterpreting what <NotAuthorized>
does?
Here’s Routes.razor:
@using DataCollection.UI.Components.Shared
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
Here’s RedirectToLogin.razor (defined in DataCollection.UI.Components.Shared):
@inject NavigationManager NavigationManager
@attribute [AllowAnonymous]
@code {
protected override void OnInitialized()
{
NavigationManager.NavigateTo($"ui/Login?returnUrl={Uri.EscapeDataString(NavigationManager.Uri)}", forceLoad: true);
}
}