Loading…
Loading…
17 карточек
How to implement authentication and authorization in Blazor?
нажми, чтобы перевернуть
AuthenticationStateProvider: provides ClaimsPrincipal. CascadingAuthenticationState: passes auth state to all components. <AuthorizeView>: shows different UI for authorized/unauthorized. [Authorize] on @page: redirect to login. Blazor Server: uses ASP.NET Core cookie auth. Blazor WASM: JWT + custom AuthStateProvider. Roles: <AuthorizeView Roles="admin">. Policy-based: <AuthorizeView Policy="CanEdit">.
<!-- App.razor -->
<CascadingAuthenticationState>
<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData"
DefaultLayout="typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
</Router>
</CascadingAuthenticationState>
<!-- Protected page -->
@page "/admin"
@attribute [Authorize(Roles = "Admin")]
<!-- Conditional UI -->
<AuthorizeView>
<Authorized>Welcome, @context.User.Identity?.Name!</Authorized>
<NotAuthorized><a href="/login">Login</a></NotAuthorized>
</AuthorizeView>Когда да
Any app with users: login, role-based UI, protected pages
Совет на собеседовании
Blazor Server auth = cookie (server-side). Blazor WASM auth = JWT (client stores token, AuthStateProvider parses claims).
Свайп вправо — знаю, влево — не знаю
