Securing pages
Protecting unauthorized routing is crucial as malicious actors can attempt to scrape your application, bypassing the navigation paths enforced by your UI. Ensuring that only authorized users can access specific routes helps safeguard sensitive data and functionality. Blazor comes with a built-in Authorize
attribute to check access permissions when a user navigates to a page.
Let’s add a routable component that only an authenticated user can navigate to by applying the Authorize
attribute in the right place.
Getting ready
Before we add a secure component to the server-side project, create a Components
/Recipes
/Recipe02
directory – this will be your working directory.
How to do it…
Follow these instructions to protect a component:
- Create a routable
Settings
component with a/
ch08r02
path:@page "/ch08r02"
- Reference the
Microsoft.AspNetCore.Authorization
assembly and attach anAuthorize
attribute to theSettings
component...