跳到主要内容

Auth 认证中心模块

Auth 模块基于 OpenIddict 7.5 实现 OAuth2/OpenID Connect 认证服务,支持授权码流、PKCE、刷新令牌。

功能概览

Auth 模块基于 OpenIddict 实现 OAuth2/OpenID Connect 认证服务,提供:

功能域能力
OAuth2 端点/connect/authorize、/connect/token、/connect/userinfo、/connect/logout
认证流程授权码流、PKCE、刷新令牌
应用管理OAuth2 客户端应用 CRUD、Scope 管理、Token 管理
声明映射动态声明映射(数据库配置)、默认 Scope(openid/profile/email/roles)
安全密码策略(5 次锁定)、证书签名、Token 撤销与清理
启动引导自动创建 Scope 和应用、非破坏性合并

快速开始

1. 配置数据库

{
"ConnectionStrings": {
"DefaultDB": "0",
"DataType": { "MySql": 0, "Sqlite": 4 },
"MySql": "server=localhost;port=3306;database=freekit;uid=root;pwd=123456;",
"Sqlite": "Data Source=freekit.auth.db"
}
}

2. 配置证书

{
"Security:OpenIddict:Certificates": {
"SigningCertificatePath": "signing.pfx",
"SigningCertificatePassword": "your-password",
"EncryptionCertificatePath": "encryption.pfx",
"EncryptionCertificatePassword": "your-password"
}
}

3. 启动服务

dotnet run --project src/Services/Auth/FreeKit.Auth.Host

架构概览

OAuth2 端点

/connect/authorize

OAuth2 授权端点,支持授权码流。

参数说明
client_id客户端 ID
redirect_uri回调地址
response_type响应类型(code)
scope请求的 Scope
code_challengePKCE 挑战
code_challenge_method挑战方法(S256)

/connect/token

令牌端点,支持授权码和刷新令牌。

参数说明
grant_type授权类型
code授权码
redirect_uri回调地址
code_verifierPKCE 验证器
refresh_token刷新令牌

/connect/userinfo

返回用户信息,基于授权的 Scope。

Scope返回的 Claims
openidsub
profilename
emailemail
rolesrole

/connect/logout

登出,撤销所有有效令牌。

核心功能

用户认证

支持用户名或邮箱登录,大小写不敏感。

认证流程

  1. 查找用户(用户名/邮箱)
  2. 检查账户锁定状态
  3. 验证密码哈希
  4. 记录失败尝试
  5. 5 次失败后锁定 30 分钟

密码策略

配置说明
最大失败次数5 次
锁定时间30 分钟
密码哈希ASP.NET Identity PasswordHasher
自动重哈希支持

动态声明映射

支持从数据库配置的动态声明映射。

映射实体

字段类型说明
ClaimTypestring声明类型
UserPropertyNamestring用户属性名
SourceTypeClaimSourceType来源类型
IsEnabledbool是否启用
Scopesstring?关联的 Scope

默认映射

声明类型来源需要的 Scope
subAuthUser.Idopenid
nameAuthUser.UserNameprofile
emailAuthUser.Emailemail
role用户角色roles

OAuth2 应用管理

管理 OAuth2 客户端应用。

应用属性

属性说明
ClientId客户端 ID
ClientSecret客户端密钥
DisplayName显示名称
ApplicationType应用类型
ConsentType同意类型
RedirectUris回调地址
PostLogoutRedirectUris登出回调
Scopes允许的 Scope
Permissions权限

Scope 管理

管理 OAuth2 Scope。

内置 Scope

Scope显示名称资源
openidOpenIDidentity
profile用户资料identity
email邮箱信息identity
roles角色信息identity
consoleConsole APIconsole
cmskitCmsKit APIcmskit
imIM APIim

Token 管理

管理访问令牌和刷新令牌。

功能说明
列出 Token分页查询
撤销 Token单个/批量撤销
清理 Token清理已兑换/已撤销的旧 Token

启动引导

OpenIddictBootstrapper 在启动时自动配置:

功能说明
确保 Scope自动创建内置 Scope
确保应用自动创建配置的应用
非破坏性合并不覆盖已有记录

API 控制器

ConnectController

方法路由认证说明
GET/connect/authorizeAnonymous授权端点
POST/connect/tokenAnonymous令牌端点
GET/connect/userinfoOpenIddict用户信息
POST/connect/logoutCookie登出

OpenIddictAdminController

方法路由认证说明
GET/api/identity/openiddict/applicationsAdmin列出应用
GET/api/identity/openiddict/applications/{id}Admin获取应用
POST/api/identity/openiddict/applicationsAdmin创建应用
PUT/api/identity/openiddict/applications/{id}Admin更新应用
DELETE/api/identity/openiddict/applications/{id}Admin删除应用
GET/api/identity/openiddict/scopesAdmin列出 Scope
POST/api/identity/openiddict/scopesAdmin创建 Scope
DELETE/api/identity/openiddict/scopes/{id}Admin删除 Scope
GET/api/identity/openiddict/tokensAdmin列出 Token
POST/api/identity/openiddict/tokens/{id}/revokeAdmin撤销 Token
POST/api/identity/openiddict/tokens/revoke-by-subjectAdmin撤销用户 Token
POST/api/identity/openiddict/tokens/revoke-by-application/{appId}Admin撤销应用 Token
POST/api/identity/openiddict/tokens/pruneAdmin清理旧 Token
GET/api/identity/openiddict/authorizationsAdmin列出授权

配置选项

数据库配置

{
"ConnectionStrings": {
"DefaultDB": "0",
"DataType": { "MySql": 0, "Sqlite": 4 },
"MySql": "server=localhost;port=3306;database=freekit_auth;uid=root;pwd=123456;",
"Sqlite": "Data Source=freekit.auth.db"
}
}

证书配置

{
"Security:OpenIddict:Certificates": {
"SigningCertificatePath": "signing.pfx",
"SigningCertificatePassword": "your-password",
"EncryptionCertificatePath": "encryption.pfx",
"EncryptionCertificatePassword": "your-password"
}
}

应用配置

{
"Security:OpenIddict:Bootstrap": {
"Enabled": true,
"Applications": [
{
"ClientId": "console",
"ClientSecret": "console-secret",
"DisplayName": "Console Application",
"RedirectUris": ["{CmsKitClient}/callback"],
"PostLogoutRedirectUris": ["{CmsKitClient}"],
"Scopes": ["openid", "profile", "email", "roles", "console", "cmskit"]
}
]
}
}

环境变量

变量说明
ASPNETCORE_ENVIRONMENT运行环境
IdentityApi认证服务地址
CmsKitClient前端客户端地址

数据库表

表名说明
identity_user用户表
identity_role角色表
identity_user_role用户角色关联
identity_connect_claim_mapping声明映射表
open_iddict_applicationOAuth2 应用表
open_iddict_authorization授权记录表
open_iddict_scopeScope 表
open_iddict_tokenToken 表

与前端集成

1. 配置回调地址

// 前端回调地址
const callbackUrl = `${authServer}/connect/callback`;

2. 获取授权码

// 重定向到授权端点
const authUrl = `${authServer}/connect/authorize?` +
`client_id=${clientId}&` +
`redirect_uri=${callbackUrl}&` +
`response_type=code&` +
`scope=openid profile email&` +
`code_challenge=${challenge}&` +
`code_challenge_method=S256`;

3. 交换令牌

const response = await fetch(`${authServer}/connect/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'authorization_code',
code: authorizationCode,
redirect_uri: callbackUrl,
code_verifier: verifier,
client_id: clientId,
client_secret: clientSecret
})
});

4. 刷新令牌

const response = await fetch(`${authServer}/connect/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: refreshToken,
client_id: clientId,
client_secret: clientSecret
})
});

相关文档