Galosys.Foundation.AspNetCore.Authorization 26.8.13.2

dotnet add package Galosys.Foundation.AspNetCore.Authorization --version 26.8.13.2
                    
NuGet\Install-Package Galosys.Foundation.AspNetCore.Authorization -Version 26.8.13.2
                    
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="Galosys.Foundation.AspNetCore.Authorization" Version="26.8.13.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.AspNetCore.Authorization" Version="26.8.13.2" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.AspNetCore.Authorization" />
                    
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 Galosys.Foundation.AspNetCore.Authorization --version 26.8.13.2
                    
#r "nuget: Galosys.Foundation.AspNetCore.Authorization, 26.8.13.2"
                    
#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 Galosys.Foundation.AspNetCore.Authorization@26.8.13.2
                    
#: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=Galosys.Foundation.AspNetCore.Authorization&version=26.8.13.2
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.AspNetCore.Authorization&version=26.8.13.2
                    
Install as a Cake Tool

Galosys.Foundation.AspNetCore.Authorization

ASP.NET Core 授权适配层(PEP,Policy Enforcement Point):将官方授权管线接线到核心引擎 Galosys.Foundation.Authorization统一授权模型。ACL / RBAC / ABAC / ReBAC 规则都表达为核心 PolicyRule,由同一个 IAuthorizationEngine 评估——本适配层不做模型区分,仅负责 Web 场景的桥接(声明式 [Authorize] / 命令式扩展 / HttpContext 环境属性源)。

设计文档:docs/designs/abac-design.md(终局设计 §3.3 双包分层);核心包(PDP)见 Galosys.Foundation.Authorization

能力清单

能力 说明
声明式授权 [Authorize(Policy="...")],policy 名即策略名,动态策略提供者无需预注册
Handler 接线 AuthorizationEngineHandler 组装核心 AuthorizationRequestIAuthorizationEngine.EvaluateAsync → 富决策 AuthorizationDecision(Succeed / Fail + DenyReason)
动态策略提供者 AuthorizationEnginePolicyProvider:任意策略名动态构造含 AuthorizationEngineRequirement 的策略
命令式扩展 IAuthorizationService.AuthorizeAsync 两个重载(ClaimsPrincipal / 显式四要素,Worker 零 HttpContext)
env 外部属性源 IEnvironmentAttributeSource / HttpContextAttributeSourceenv.SourceIp/env.UserAgent),随 AddAuthorizationEngine() 自动装配(Worker 下 HttpContext 为 null 安全回退)
组合装配(自含) AddAuthorizationEngine() = 官方 AddAuthorization()IAuthorizationService)+ 核心 AddAuthorizationEngineCore() + 适配组件,HttpContext 属性源自动注册,全部 TryAdd 可覆盖
策略存储桥 「引入包即替换」:RemoveAll<IAuthorizationPolicyRepository> + Add<T>() 替换核心仓库(业务桥实现核心接口,动态模式)

统一模型(ACL / RBAC / ABAC 无需区分)

适配层背后的核心引擎不区分规则类型——ACL 即 Conditions 为空的 PolicyRule

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": ["user:alice"],
      "Action": ["doc:Read"],
      "Resource": ["acs:doc:*:*:report/123"]
    }
  ]
}

PolicyRule 可选 Category 元数据("acl"/"rbac"/"abac"/"rebac"),仅用于管理界面分类,引擎评估忽略。Agent / Worker 直接引用核心包即可评估全部模型,无需任何额外组件。

使用示例

注册(Web,自含装配)

services.AddAuthorizationEngine(options =>
{
    options.PollingInterval = TimeSpan.FromSeconds(30);   // 热更新轮询
    options.Policies = new[] { policyRule };              // 核心 AuthorizationEngineOptions,或 PolicyFilePath / PolicySection
});
// 自含:官方 IAuthorizationService、动态策略提供者、授权处理器、HttpContext 环境属性源均已自动注册,无需额外调用

声明式(接口级授权)

[Authorize(Policy = "orders:edit")]   // policy 名即策略名,无需预注册
public async Task<UnifiedResponse> EditOrder(OrderUpdateCommand command) { ... }

命令式(资源级)

var result = await _authorizationService.AuthorizeAsync(User, order, "orders:approve");
if (!result.Succeeded) return UnifiedResponse.Fail("无权限");

显式四要素(Worker/Agent,零 HttpContext)

var decision = await _authorizationService.AuthorizeAsync(
    new Subject("alice").WithAttr("roleIds", new[] { "admin" }),
    order, "orders:edit",
    new EnvironmentContext().WithAttr("SourceIp", "10.1.2.3"));

替换策略存储(业务桥,引入包即替换)

services.AddAuthorizationEngine();
services.RemoveAll<IAuthorizationPolicyRepository>();
services.AddSingleton<IAuthorizationPolicyRepository, BopAbacPolicyStore>();   // 实现核心接口(动态模式)

扩展点契约:核心注册全 TryAdd(宿主先注册不被覆盖);集成包在基础之后以 RemoveAll + Add 替换仓库/引擎,或以 TryAddEnumerable 追加属性提供者。Casbin 引擎替换、DataPermission 属性提供者均按此契约由独立集成包装配。

HttpContext 环境属性源(自动装配)

services.AddAuthorizationEngine();   // env.SourceIp / env.UserAgent 自动回退 HttpContext,无需显式装配

引擎直用(低层 API,零 Web)

var engine = sp.GetRequiredService<IAuthorizationEngine>();
var decision = await engine.EvaluateAsync(new AuthorizationRequest
{
    Subject = new Subject("alice"),
    Resource = new Resource("acs:doc:*:*:report/123"),
    Action = new Action("doc:Read"),
});
if (decision.IsAllowed) { /* 执行 */ }

依赖

  • Galosys.Foundation.Authorization(核心 PDP,零 Http 引用)
  • Galosys.Foundation.Core
  • Microsoft.AspNetCore.Authorization(官方授权抽象)
  • Microsoft.AspNetCore.Http(PEP 适配层必须引用 IHttpContextAccessor/HttpContextAccessor;已移除 Microsoft.AspNetCore.Http.Abstractions 直接引用)
  • Microsoft.Extensions.DependencyInjection / Microsoft.Extensions.Logging

不依赖 Galosys.Foundation.AspNetCore 模块与 Galosys.Foundation.DataPermission(数据权限 / Casbin 为独立集成包的显式装配)。

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
26.8.13.2 0 8/13/2026
26.8.13.1 0 8/13/2026
26.8.12.2 37 8/12/2026
26.8.12.1 37 8/12/2026
26.8.10.1 84 8/10/2026
26.8.5.1 86 8/5/2026
26.8.4.1 81 8/4/2026
26.8.3.1 100 8/3/2026
26.7.31.1 94 7/31/2026