SyZero.Web.Common 1.2.0-dev.1

This is a prerelease version of SyZero.Web.Common.
There is a newer version of this package available.
See the version list below for details.
dotnet add package SyZero.Web.Common --version 1.2.0-dev.1
                    
NuGet\Install-Package SyZero.Web.Common -Version 1.2.0-dev.1
                    
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="SyZero.Web.Common" Version="1.2.0-dev.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.Web.Common" Version="1.2.0-dev.1" />
                    
Directory.Packages.props
<PackageReference Include="SyZero.Web.Common" />
                    
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 SyZero.Web.Common --version 1.2.0-dev.1
                    
#r "nuget: SyZero.Web.Common, 1.2.0-dev.1"
                    
#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 SyZero.Web.Common@1.2.0-dev.1
                    
#: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=SyZero.Web.Common&version=1.2.0-dev.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SyZero.Web.Common&version=1.2.0-dev.1&prerelease
                    
Install as a Cake Tool

SyZero.Web.Common

SyZero 框架的 Web 通用组件模块,提供 JWT 认证、响应包装等功能。

📦 安装

dotnet add package SyZero.Web.Common

✨ 特性

  • 🔒 JWT 认证 - 完整的 JWT Token 生成和验证
  • 📦 统一响应 - 统一的 API 响应格式
  • 🎯 请求上下文 - 当前用户和请求上下文管理
  • ⚠️ 异常过滤器 - 统一的异常处理

🚀 快速开始

1. 配置 appsettings.json

{
  "Jwt": {
    "SecretKey": "your-secret-key-at-least-32-characters",
    "Issuer": "SyZero",
    "Audience": "SyZero",
    "ExpireMinutes": 120
  }
}

2. 注册服务

// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();

// 注册服务方式1 - 使用配置文件
builder.Services.AddSyZeroWebCommon();

// 注册服务方式2 - 使用委托配置
builder.Services.AddSyZeroWebCommon(options =>
{
    options.SecretKey = "your-secret-key-at-least-32-characters";
    options.ExpireMinutes = 120;
});

// 注册服务方式3 - 禁用某些功能
builder.Services.AddSyZeroWebCommon(options =>
{
    options.EnableJwt = true;
    options.EnableExceptionFilter = true;
});

var app = builder.Build();
// 使用SyZero
app.UseSyZero();
// 使用认证
app.UseAuthentication();
app.UseAuthorization();
app.Run();

3. 使用示例

// 生成 Token
public class AuthService
{
    private readonly IJwtTokenGenerator _tokenGenerator;

    public AuthService(IJwtTokenGenerator tokenGenerator)
    {
        _tokenGenerator = tokenGenerator;
    }

    public string GenerateToken(User user)
    {
        return _tokenGenerator.GenerateToken(new Dictionary<string, string>
        {
            ["userId"] = user.Id.ToString(),
            ["userName"] = user.Name
        });
    }
}

// 获取当前用户
public class UserService
{
    private readonly ICurrentUser _currentUser;

    public UserService(ICurrentUser currentUser)
    {
        _currentUser = currentUser;
    }

    public long GetCurrentUserId()
    {
        return _currentUser.Id ?? throw new UnauthorizedAccessException();
    }
}

📖 配置选项

属性 类型 默认值 说明
SecretKey string "" JWT 密钥(至少32字符)
Issuer string "" 签发者
Audience string "" 接收者
ExpireMinutes int 120 过期时间(分钟)

📖 API 说明

ICurrentUser 接口

属性/方法 说明
Id 当前用户 ID
Name 当前用户名
IsAuthenticated 是否已认证
GetClaimValue(type) 获取指定声明值

IJwtTokenGenerator 接口

方法 说明
GenerateToken(claims) 生成 JWT Token
ValidateToken(token) 验证 Token

使用标准的 ASP.NET Core 认证中间件


🔧 高级用法

自定义 Token 声明

var token = _tokenGenerator.GenerateToken(new Dictionary<string, string>
{
    ["userId"] = user.Id.ToString(),
    ["role"] = user.Role,
    ["permissions"] = string.Join(",", user.Permissions)
});

刷新 Token

public class AuthController : ControllerBase
{
    [HttpPost("refresh")]
    public async Task<TokenResult> RefreshAsync(string refreshToken)
    {
        // 验证 refresh token
        // 生成新的 access token
    }
}

⚠️ 注意事项

  1. 密钥安全 - JWT 密钥必须保密且足够复杂
  2. Token 过期 - 合理设置 Token 过期时间
  3. 中间件顺序 - Authentication 必须在 Authorization 之前

📄 许可证

MIT License - 详见 LICENSE

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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 (4)

Showing the top 4 NuGet packages that depend on SyZero.Web.Common:

Package Downloads
SyZero.AspNetCore

SyZero中的Web层扩展

SyZero.Feign

SyZero-Feign

SyZero.Example2.IApplication

Package Description

SyZero.Example1.IApplication

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.1.0 30 10/22/2026
1.2.0 39 10/22/2026
1.2.0-dev.1 20 10/22/2026
1.1.9 190 4/19/2026
1.1.9-dev.2 76 4/19/2026
1.1.9-dev.1 84 4/17/2026
1.1.8 158 4/17/2026
1.1.6 157 4/17/2026
1.1.6-dev.1 73 4/17/2026
1.1.5 166 4/13/2026
1.1.5-dev.3 84 4/13/2026
1.1.5-dev.2 109 2/11/2026
1.1.5-dev.1 87 1/29/2026
1.1.4 194 1/2/2026
1.1.4-dev.2 101 1/2/2026
1.1.4-dev.1 88 12/30/2025
1.1.3 206 12/30/2025
1.1.3-dev.6 103 12/30/2025
1.1.3-dev.3 157 1/19/2024
1.0.0 218 4/17/2026
Loading failed