Galosys.Foundation.AspNetCore 26.7.18.2

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

Galosys.Foundation.AspNetCore

目标框架: net10.0

成熟度: 🟢 稳定 — 生产可用,测试充分,活跃维护

简介

Galosys.Foundation.AspNetCore 是 ASP.NET Core 核心扩展模块,提供 MVC 过滤器、身份认证、权限管理、SignalR 中间件等功能。

特性

MVC 过滤器

  • GlobalResponseLoggingResultFilter - 响应日志记录
  • GlobalLogExceptionFilter - 全局异常日志
  • GlobalModelStateValidationActionFilter - 模型状态验证
  • AuditLogAttribute - 审计日志
  • RequestLogAttribute - 请求日志
  • CacheableAttribute - 响应缓存(支持多租户 Key 隔离,HybridCache 内置击穿保护,可选布隆过滤器)
  • CacheEvictAttribute - 缓存清除(支持多租户 Key 隔离)
  • CachePutAttribute - 缓存写入(支持多租户 Key 隔离)
  • SensitiveAttribute + SensitiveDataFilter - API 响应敏感字段自动脱敏(手机/身份证/邮箱等)
  • RateLimitAttribute - 声明式限流元数据,配合 GlobalLimiter 按用户/租户粒度限流
  • IdempotentAttribute - 幂等性验证

身份认证

  • JWT Bearer - JWT 认证支持
  • HttpBasic - HTTP 基本认证
  • ApiKey - API Key 认证

权限管理

  • PermissionAuthorizationHandler - 权限授权处理器
  • PermissionScanner - 权限扫描器
  • DynamicAuthorizationPolicyProvider - 动态授权策略

SignalR

  • SignalR Hub - 实时通信
  • SignalR Client - 客户端支持

中间件

  • CorrelationIdMiddleware - 跨服务请求链路 CorrelationId 生成与透传
  • GlobalExceptionHandlerMiddleware - 全局异常处理
  • MultiTenancyMiddleware - 多租户支持
  • LoginUserMiddleware - 登录用户中间件
  • CorsMiddleware - CORS 配置
  • SensitiveDataFilter - API 响应敏感字段自动脱敏(IAlwaysRunResultFilter)

安装

dotnet add package Galosys.Foundation.AspNetCore

使用

注册服务

services.AddAspNetCore(); // 注册所有服务

配置 JWT 认证

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Authority = "https://auth.example.com";
        options.Audience = "api.example.com";
    });

配置 SignalR

services.AddSignalR();

app.UseEndpoints(endpoints =>
{
    endpoints.MapHubs();
});

使用审计日志

[AuditLog("创建订单")]
public async Task<IActionResult> CreateOrder([FromBody] CreateOrderRequest request)
{
    // 业务逻辑,审计日志自动通过 ReliableLogChannel 异步持久化
}

// 设置审计前后值
HttpContext.SetAuditValues(originalValues, currentValues, targetId);
审计日志持久化

审计日志通过 Tier 2 可靠通道异步持久化,失败自动重试(指数退避,最多 3 次)。需注册通道和实现 IAuditLogRepository

// 注册通道 + 消费者(通常在 Startup 中)
services.AddAuditLogChannel();

// 实现持久化接口
public class AuditLogRepository : IAuditLogRepository
{
    public Task AddAsync(AuditLog auditLog, CancellationToken ct = default)
    {
        // 写入 lc.lc_audit_log 表
    }
}

使用请求日志

[RequestLog("订单API")]
public async Task<IActionResult> ListOrders([FromQuery] OrderQuery query)
{
    // 请求日志自动通过 ObservableLogChannel 批量持久化
}
请求日志持久化

请求日志通过 Tier 1 可观测通道批量持久化。需注册通道和实现 IRequestLogRepository

services.AddRequestLogChannel();

public class RequestLogRepository : IRequestLogRepository
{
    public Task AddRangeAsync(IReadOnlyList<RequestLogCreateInput> inputs, CancellationToken ct = default)
    {
        // 批量写入 lc.lc_api_request_log 表
    }
}

使用登录日志

// 替代旧版 GetUserLoginEvent + publisher.PublishAsync
await HttpContext.PublishLoginLogAsync(realName, status: 1, remark: "登录成功");

登录日志通过 Tier 2 可靠通道持久化。需注册通道和实现 ILoginLogRepositoryAddCore() 内置注册通道)。

使用 CorrelationId 链路透传

CorrelationIdMiddleware 已自动集成在 UseAspNetCore() 管道中(位于 UseLocalization() 之后),无需手动注册。支持自定义 Header 名称:

app.UseCorrelationId(options =>
{
    options.Header = "X-Request-Id";          // 读取的请求 Header
    options.ResponseHeader = "X-Request-Id";   // 写入的响应 Header
    options.IncludeInResponse = true;           // 是否在响应中包含
});

CorrelationId 自动写入 HttpContext.Items["CorrelationId"] 和日志上下文(ILogger.BeginScope)。

使用敏感数据脱敏

在 Model 属性上标注 [Sensitive],API 返回时自动脱敏:

public class UserDto
{
    public string Name { get; set; }
    [Sensitive(SensitiveDataType.Phone)]
    public string Phone { get; set; }
    [Sensitive(SensitiveDataType.IdCard)]
    public string IdCard { get; set; }
    [Sensitive(SensitiveDataType.Email)]
    public string Email { get; set; }
}

Program.cs 中注册:

builder.Services.AddSensitiveDataFilter(options =>
{
    options.MaxDepth = 5; // 递归扫描最大深度,默认 5
});

使用幂等性验证

[Idempotent]
public async Task<IActionResult> SubmitOrder([FromBody] SubmitOrderRequest request)
{
    // 幂等操作
}

使用 RateLimit 声明式限流

[RateLimit] 配合内置 GlobalLimiter 自动生效,无需额外注册。

[RateLimit(By = "User", Limit = 10, WindowSeconds = 60)]
public IActionResult Get() => Ok();

[RateLimit(By = "Tenant", Limit = 100, WindowSeconds = 60)]
public IActionResult List() => Ok();

使用缓存 Attribute(基于 HybridCache)

// 基本用法 — Cache Key: "dict-type:{表达式结果}",HybridCache 自动管理 L1/L2 缓存和击穿保护
[Cacheable("dict-type", Key = "query.Code")]
public async Task<UnifiedResponse> GetDictType(DictTypeQuery query) { }

// 多租户隔离 — Cache Key: "dict-type:{tenantId}:{表达式结果}"
[Cacheable("dict-type", Key = "query.Code", TenantAware = true)]
public async Task<UnifiedResponse> GetDictType(DictTypeQuery query) { }

// 布隆过滤器防穿透 — 缓存键不存在时直接返回,跳过 DB
[Cacheable("dict-type", Key = "query.Code", EnableBloomFilter = true)]
public async Task<UnifiedResponse> GetDictType(DictTypeQuery query) { }

// 缓存清除
[CacheEvict("dict-type", Key = "command.Code", TenantAware = true)]
public async Task<UnifiedResponse> UpdateDictType(UpdateDictTypeCommand command) { }

// 缓存写入
[CachePut("dict-type", Key = "command.Code", TenantAware = true)]
public async Task<UnifiedResponse> CreateDictType(CreateDictTypeCommand command) { }
TenantAware Key 格式
场景 TenantAware TenantId Key 示例
不感知(默认) false dict-type:GZ
感知 + 有租户 true 5 dict-type:5:GZ
感知 + 无租户 true 0 dict-type:global:GZ
感知 + 无 Key 表达式 true 5 dict-type:5

前提:使用 TenantAware=true 需确保已调用 services.AddCore() 注册 ITenantContext

权限控制

[Authorize(Policy = "RequirePermission")]
public async Task<IActionResult> DeleteOrder(string orderNo)
{
    // 需要特定权限
}

SignalR Hub

public class NotificationHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.User(user).SendAsync("ReceiveMessage", message);
    }
}

核心类

分类 说明
Filter GlobalResponseLoggingResultFilter 响应日志过滤器
Filter GlobalLogExceptionFilter 异常日志过滤器
Filter AuditLogAttribute 审计日志特性
Filter RateLimitAttribute 声明式限流元数据
Auth JwtHelper JWT 辅助类
Auth HttpBasicAuthenticationHandler HTTP 基本认证
SignalR HubBase Hub 基类
Middleware GlobalExceptionHandlerMiddleware 全局异常处理

依赖

  • Microsoft.AspNetCore.Authentication.JwtBearer
  • Microsoft.AspNetCore.SignalR
  • Galosys.Foundation.Core
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 (9)

Showing the top 5 NuGet packages that depend on Galosys.Foundation.AspNetCore:

Package Downloads
Galosys.Foundation.AspNetCore.DynamicApi

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.Quartz

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.AdminSafe

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.Localization

Galosys.Foundation快速开发库

Galosys.Foundation.AspNetCore.HealthChecks.UI

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.7.18.2 0 7/18/2026
26.7.18.1 0 7/18/2026
26.7.17.1 38 7/17/2026
26.7.14.1 157 7/14/2026
26.7.13.1 222 7/13/2026
26.7.12.1 224 7/12/2026
26.7.11.1 226 7/11/2026
26.7.10.2 222 7/10/2026
26.7.10.1 228 7/10/2026
26.7.7.2 224 7/7/2026
26.7.7.1 227 7/7/2026
26.7.2.1 228 7/2/2026
26.7.1.2 231 7/1/2026
26.5.20.1 358 5/20/2026
26.5.19.1 348 5/19/2026
26.5.18.1 348 5/18/2026
26.5.15.1 348 5/15/2026
26.5.12.3 338 5/12/2026
26.5.12.2 359 5/12/2026
1.0.0 341 7/7/2026
Loading failed