configure-auth

仓库创建 2026年2月4日最近提交 18 小时前SkillHot 收录 20 天前
▸ 精选理由

覆盖登录登出、AuthorizeView 与 AuthenticationStateProvider 等常见需求。

这个 Skill 做什么

为 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).

开发编程身份验证Blazor授权通用
4.8k
Stars
351
Forks
40
仓库内 Skill
+610
7 日增星
安装 / 使用
给你的 Agent 一句话(通用)
帮我安装这个 skill:https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-blazor/skills/configure-auth/SKILL.md
或 curl 直取 SKILL.md
curl -fsSL "https://raw.githubusercontent.com/dotnet/skills/main/plugins/dotnet-blazor/skills/configure-auth/SKILL.md"
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>()
    .
via SKILL·HOT · 数据来自 GitHub 公开信息 · 原文版权归作者所有