OneAuth.Client.AspNetCore 0.4.0

dotnet add package OneAuth.Client.AspNetCore --version 0.4.0
                    
NuGet\Install-Package OneAuth.Client.AspNetCore -Version 0.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OneAuth.Client.AspNetCore" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OneAuth.Client.AspNetCore" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="OneAuth.Client.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OneAuth.Client.AspNetCore --version 0.4.0
                    
#r "nuget: OneAuth.Client.AspNetCore, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OneAuth.Client.AspNetCore@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OneAuth.Client.AspNetCore&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=OneAuth.Client.AspNetCore&version=0.4.0
                    
Install as a Cake Tool

OneAuth.Client.AspNetCore

OneAuth 统一身份权限中心的 ASP.NET Core 业务系统接入 SDK。

  • 按用户有效权限注水:验签后由 PermissionClaimsTransformer 把用户在本系统的有效权限(直接授权 ∪ 角色授权,中心物化)以 permissions claim 注入 ClaimsPrincipal,FastEndpoints 的 Permissions(...) / 标准授权检查照常工作,业务代码零改动。
  • EffectivePermissionCache:IMemoryCache TTL 缓存(默认 2 分钟)+ Redis perm-invalidate 精确失效 + 中心不可达时沿用过期缓存(stale-while-error)。
  • 权限树契约接口:MapOneAuthPermissionContract 暴露本系统权限点树,供权限中心拉取同步。
  • 约定式接入:AddOneAuthJwtBearer() + AddOneAuthEffectivePermissions() 两行完成,配置从 OneAuth 节读取。

安装

dotnet add package OneAuth.Client.AspNetCore

目标框架:net10.0。

快速接入

1. 配置(appsettings.json)

{
  "OneAuth": {
    "Keycloak": {
      "Authority": "http://localhost:8080/realms/corp",
      "Audience": "asset-admin",
      "TokenUrl": "http://localhost:8080/realms/corp/protocol/openid-connect/token",
      "ClientId": "asset-admin-svc",
      "ClientSecret": "asset-admin-svc-secret"
    },
    "PermissionCenter": {
      "Url": "http://localhost:5511",
      "Redis": "localhost:6379"
    }
  }
}
配置项 必填 说明
OneAuth:Keycloak:Authority 是 Keycloak realm 地址,JWT 验签的 Authority
OneAuth:Keycloak:Audience 是 本系统 api 侧 clientId,须与 Keycloak audience mapper 一致;同时默认作为 SystemId
OneAuth:Keycloak:RequireHttpsMetadata 否 缺省 true,仅开发环境可配 false
OneAuth:Keycloak:TokenUrl 是 token 端点,供本系统 service account 走 client_credentials 调权限中心
OneAuth:Keycloak:ClientId 是 service account clientId(约定 {systemId}-svc)
OneAuth:Keycloak:ClientSecret 是 service account clientSecret
OneAuth:PermissionCenter:Url 是 权限中心基础地址
OneAuth:PermissionCenter:Redis 否 Redis 连接串,用于订阅权限失效通知;不配置则只靠 TTL 兜底
OneAuth:PermissionCenter:ContractKey 否 权限树契约接口的共享密钥。配置后 MapOneAuthPermissionContract 要求请求头 X-OneAuth-Contract-Key 匹配(否则 401);不配置则匿名可访问并打警告日志。须与权限中心侧约定:中心拉取时优先用系统注册信息里的 ContractKey,空则回退中心配置 PermissionTreeSync:GlobalContractKey

2. 注册(Program.cs)

var builder = WebApplication.CreateBuilder(args);

builder.AddOneAuthJwtBearer();   // JWT 验签(MapInboundClaims=false 由 SDK 固定)
builder.AddOneAuthEffectivePermissions(); // 有效权限缓存 + claims 注水

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

// 可选:暴露权限树契约接口,供权限中心拉取本系统权限点
// (配置了 OneAuth:PermissionCenter:ContractKey 时要求请求头 X-OneAuth-Contract-Key 匹配)
app.MapOneAuthPermissionContract(tree);

app.Run();

之后端点上直接声明权限点即可(FastEndpoints 示例):

public class ExportEndpoint : EndpointWithoutRequest
{
    public override void Configure()
    {
        Post("/api/assets/export");
        Permissions("assets.export");
    }
}

工作原理

JWT 验签通过
   │
   ▼
PermissionClaimsTransformer  ──► 读 sub
   │
   ▼
EffectivePermissionCache.GetAsync(userId)
   │  命中 IMemoryCache(TTL 默认 2min)──► 直接返回
   │  未命中 ──► client_credentials 换 token
   │          ──► GET {中心}/api/systems/{systemId}/users/{userId}/permissions
   │          ──► 中心不可达:沿用最后一次成功值(stale-while-error)
   ▼
权限点以 "permissions" claim 注入 ClaimsPrincipal
   │
   ▼
FastEndpoints Permissions(...) / 标准授权检查

权限变更即时生效:权限中心重算物化权限后向 Redis perm-invalidate 频道广播,SDK 后台服务收到通知后精确失效对应用户的本地缓存(systemId 匹配或 * 全量)。

手动失效

需要时可注入 EffectivePermissionCache 主动失效:

cache.Invalidate(userId);  // 精确失效某用户
cache.InvalidateAll();     // 清空本系统全部条目(不影响宿主应用其他缓存)

systemId 缺省取 OneAuth:Keycloak:Audience,也可显式传入:builder.AddOneAuthEffectivePermissions("my-system")。

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 101 10/11/2026
0.3.0 89 10/11/2026

0.4.0:契约接口支持共享密钥校验(MapOneAuthPermissionContract 读 OneAuth:PermissionCenter:ContractKey 或显式传参,要求请求头 X-OneAuth-Contract-Key 匹配,否则 401;未配置保持匿名并打警告日志);AddOneAuthPermissions 更名为 AddOneAuthEffectivePermissions(破坏性改名,无兼容别名)。0.3.0:新增约定式接入扩展 AddOneAuthJwtBearer / AddOneAuthPermissions(读配置节,SystemId 缺省取 Keycloak:Audience),MapInboundClaims=false 由 SDK 固定。0.2.0:切换到按用户有效权限模式(中心物化表),移除 PermissionMapService 与对 JWT roles claim 的依赖;调中心运行时接口改用 client_credentials(新增 TokenUrl/ClientId/ClientSecret 配置项)。