Securing Blazor Server
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 a 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 an AuthorizeRouteView
, which can have different views depending on whether or not you are authenticated:
- In the end, the
App.razor
component should look like this:<CascadingAuthenticationState> <Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="new[] { typeof(Components.Pages.Index).Assembly}"> <Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <Authorizing> <p>Determining session state, please wait...</p> ...