Application Security in Blazor
Application Security is the seventh chapter of the free Blazor tutorial. It shows how requests reach your app and how to keep the app safe.
What each lesson covers:
- Middleware Pipeline: Middleware and the request pipeline, configuring it in Program.cs, execution order, built-in middleware (static files, HTTPS redirection, exception handling, status code pages), Use, Run and Map, short-circuiting, and custom middleware (inline and classes).
- Authentication: What authentication is, cookies (managing them, HttpOnly, Secure, SameSite and Expires, cookies in Blazor), the cookie authentication scheme and claims, ASP.NET Core Identity, password storage, register, login and logout, AuthenticationStateProvider, cascading authentication state and ClaimsPrincipal.
- Authorization: Protecting pages with [Authorize], AuthorizeRouteView, redirecting users to login, AuthorizeView with Authorized and NotAuthorized content, role-based and claims-based authorization, protecting API endpoints, and 401 vs 403.
- Threat Protection: Common web threats, XSS and rendering untrusted HTML, CSRF and antiforgery tokens, HTTPS, HSTS and CORS policies, keeping secrets out of WebAssembly, and server-side validation.
Every code box compiles your components with the real Razor compiler and runs them on .NET 10 in your browser.
Lessons in this section
- Middleware Pipeline
How every request passes through middleware: the ASP.NET Core request pipeline, Program.cs setup, execution order, built-in middleware (static files, HTTPS redirection, exception handling, status code pages), Use, Run and Map, short-circuiting, and inline and class middleware.
- Explain how a request travels through the middleware pipeline
- Configure and order middleware in Program.cs
- Use Use, Run and Map, and short-circuit the pipeline
- Write custom middleware inline and as a class
- Authentication
Find out who the user is: cookies and their security options, cookies in Blazor, the cookie authentication scheme and claims, ASP.NET Core Identity, password hashing with salt, register, login and logout, AuthenticationStateProvider, cascading authentication state and ClaimsPrincipal.
- Explain cookies and set HttpOnly, Secure, SameSite and expiry
- Describe the cookie scheme, claims and ASP.NET Core Identity
- Explain how passwords are stored and how login and logout work
- Read the current user with AuthenticationStateProvider and cascading state
- Authorization
Decide what a user may do: protecting pages with [Authorize], AuthorizeRouteView, redirecting to login, AuthorizeView with Authorized and NotAuthorized content, role-based and claims-based authorization with policies, protecting API endpoints, and 401 vs 403.
- Protect pages with [Authorize] and AuthorizeRouteView
- Redirect users to login and show content with AuthorizeView
- Use role-based and claims-based authorization
- Protect API endpoints and tell 401 from 403
- Threat Protection
Protect a Blazor app from common web threats: XSS and rendering untrusted HTML, CSRF and antiforgery tokens, HTTPS, HSTS and CORS policies, keeping secrets out of WebAssembly, and server-side validation.
- Explain common web threats and the default protections
- Prevent XSS and render untrusted HTML safely
- Explain CSRF and how antiforgery tokens stop it
- Use HTTPS, HSTS and CORS, keep secrets on the server and validate on the server