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
BlazorWebAppproject, openComponents/_Imports.razorand add the namespaces:@using Microsoft.AspNetCore.Components.Authorization @using BlazorWebApp.Components.Layout @using BlazorWebApp.Components - Open the
Components/Routes.razorcomponent and replace everything inside theRoutercomponent with the following:<Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <Authorizing> <p>...