Skip to content
Bitzsoft.Integrationsbitzsoft.integrations

Reference

Auth0 IdentityProvider

Auth0 身份提供者服务——OAuth2 client_credentials 令牌模式,Management API。

Last updated

基于 Auth0 Management API 实现,采用 OAuth2 Client Credentials 令牌模式,内置令牌缓存与自动刷新。

包信息

  • NuGet 包:Bitzsoft.Integrations.IdentityProvider.Auth0
  • 接口:IIdentityProviderService
  • 鉴权:OAuth2 client_credentials(ClientId / ClientSecret)
  • Provider ID:auth0

配置

字段类型必填默认值说明
ClientIdstring客户端 ID
ClientSecretstring客户端密钥
DomainstringAuth0 租户域名(形如 https://dev-example.auth0.com
Audiencestring?令牌请求的 Audience(Management API 标识,留空时自动拼接)
HttpClientNamestringIdentityProviderAuth0业务 HttpClient 命名
TokenHttpClientNamestringIdentityProviderAuth0Token令牌刷新专用 HttpClient 命名
{
"IdentityProvider": {
"Auth0": {
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret",
"Domain": "https://dev-example.auth0.com"
}
}
}

注册

// 方式一:IConfiguration(默认读取 "IdentityProvider:Auth0" 配置节)
services.AddAuth0IdentityProvider(configuration);
// 方式二:委托配置
services.AddAuth0IdentityProvider(options =>
{
options.ClientId = "your-client-id";
options.ClientSecret = "your-client-secret";
options.Domain = "https://dev-example.auth0.com";
});

使用示例

// 创建用户
var user = await idp.CreateUserAsync(new CreateUserRequest
{
Email = "carol@example.com",
Login = "carol@example.com"
});
// 查询用户列表
var users = await idp.ListUsersAsync(new ListUsersRequest
{
Search = "email:carol@example.com"
});
// 停用用户
await idp.DeactivateUserAsync(user.UserId);
// 移出用户组
await idp.RemoveUserFromGroupAsync(new UserGroupRequest
{
UserId = user.UserId,
GroupId = "rol_abc123"
});

鉴权机制

  • 令牌获取:POST https://{tenant}.auth0.com/oauth/token,form-urlencoded,grant_type=client_credentialsaudience 指向 Management API。
  • 令牌缓存:Auth0TokenManager 为 Singleton,过期前 5 分钟刷新。
  • 业务请求:按请求注入 Authorization: Bearer {token} 头,访问 /api/v2/... 端点。
  • 业务 HttpClient 的 BaseAddress 配置为 {Domain}/
  • 双 HttpClient 隔离:令牌刷新 HttpClient 不经业务管道。

已知限制

凭据需在 Auth0 应用管理页创建 Machine-to-Machine Application 后获取。Audience 必须指向 Management API(留空时由系统自动拼接 https://{tenant}.auth0.com/api/v2/)。Provider 注册为 Transient。

100%

滚轮或按钮缩放 · 放大后拖动画面 · 双击切换 100% / 200%