To redirect to the login page when a user is not authenticated in Blazor WebAssembly:
- Create a login page component for redirection.
[LoginRedirect.razor]@inject NavigationManager UriHelper @code { protected override void OnInitialized() { UriHelper.NavigateTo("login"); } } - Now add the LoginRedirect component to the NotAuthorized tag to redirect to the login page if the user is not authorized.
[App.razor]Refer to this documentation for more details.<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true"> <Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <NotAuthorized> <LoginRedirect /> </NotAuthorized> </AuthorizeRouteView> </Found> <NotFound> <LayoutView Layout="@typeof(MainLayout)"> <p>Sorry, there's nothing at this address.</p> </LayoutView> </NotFound> </Router>
View Sample in GitHub
-
Create a login page component.
-
Add the login page component to the NotAuthorized tag.


Share with