Securing our Blazor app
Blazor uses App.razor
for routing. To enable securing Blazor, we need to add a couple of components in the app component.
We need to add CascadingAuthenticationState
, which will send the authentication state to all the components that are listening for it. We also need to change the route view to AuthorizeRouteView
, which can have different views depending on whether or not you are authenticated:
- In the
BlazorWebApp
project, openComponents/_Imports.razor
and add the namespaces:@using Microsoft.AspNetCore.Components.Authorization @using BlazorWebApp.Components.Layout @using BlazorWebApp.Components
- Open the
Components/Routes.razor
component and replace everything inside theRouter
component with the following:<Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <Authorizing> <p>...