configure-auth
覆盖登录登出、AuthorizeView 与 AuthenticationStateProvider 等常见需求。
为 Blazor Web 应用配置身份验证与授权,兼顾不同渲染模式。
为 Blazor Web 应用接入鉴权和授权,兼顾不同渲染模式(Server、WASM、预渲染)带来的细节差异。适合需要在页面用 [Authorize]、AuthorizaView、角色/策略控制或实现登录登出、AuthenticationStateProvider 等场景。优势是处理了常见的鉴权坑位,比如 WASM 加载后 auth state 为 null、交互组件中 SignInManager 问题以及预渲染下的授权异常。
▸ 展开 SKILL.md 英文原文
Add authentication and authorization to a Blazor Web App, accounting for the app's render mode. USE WHEN the user needs [Authorize] on pages, AuthorizeView, role or policy-based access, login/logout Identity pages, or AuthenticationStateProvider. Also USE WHEN auth state is null after WebAssembly loads, SignInManager throws in an interactive component, <NotAuthorized> content never renders in static SSR, or HttpContext.User is null in an interactive component. DO NOT USE for general component authoring (see author-component), for prerendering concerns unrelated to auth (see support-prerendering), or for managing non-auth cascading state (see coordinate-components).
帮我安装这个 skill:https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-blazor/skills/configure-auth/SKILL.mdcurl -fsSL "https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-blazor/skills/configure-auth/SKILL.md"# Configure Auth
## Step 1 — Read AGENTS.md
Read `AGENTS.md` at the workspace root for the project's interactivity mode and scope before making changes.
## Step 2 — Register auth services in Program.cs
```csharp
// Program.cs (server project)
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddAuthorization();
```
For ASP.NET Core Identity add the Identity services:
```csharp
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();
builder.Services.AddIdentityCore<ApplicationUser>()
.AddRoles<IdentityRole>()
.