SyZero.Web.Common
1.1.4
dotnet add package SyZero.Web.Common --version 1.1.4
NuGet\Install-Package SyZero.Web.Common -Version 1.1.4
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.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.Web.Common" Version="1.1.4" />
<PackageReference Include="SyZero.Web.Common" />
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.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.Web.Common, 1.1.4"
#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.1.4
#: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.1.4
#tool nuget:?package=SyZero.Web.Common&version=1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
}
}
⚠️ 注意事项
- 密钥安全 - JWT 密钥必须保密且足够复杂
- Token 过期 - 合理设置 Token 过期时间
- 中间件顺序 - Authentication 必须在 Authorization 之前
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Aliyun.OSS.SDK.NetCore (>= 2.14.1)
- Microsoft.AspNetCore.Http.Features (>= 3.1.32)
- Newtonsoft.Json (>= 13.0.1)
- RestSharp (>= 112.1.0)
- System.IdentityModel.Tokens.Jwt (>= 8.3.0)
- SyZero (>= 1.1.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on SyZero.Web.Common:
| Package | Downloads |
|---|---|
|
SyZero.AspNetCore
SyZero中的Web层扩展 |
|
|
SyZero.Feign
SyZero-Feign |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.4 | 113 | 1/2/2026 |
| 1.1.4-dev.2 | 45 | 1/2/2026 |
| 1.1.4-dev.1 | 40 | 12/30/2025 |
| 1.1.3 | 132 | 12/30/2025 |
| 1.1.3-dev.6 | 52 | 12/30/2025 |
| 1.1.3-dev.3 | 115 | 1/19/2024 |
| 1.1.3-dev.2 | 173 | 11/3/2023 |
| 1.1.3-dev.1 | 182 | 3/21/2023 |
| 1.1.2 | 787 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 177 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 167 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 172 | 3/15/2023 |
| 1.1.1 | 727 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 171 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 181 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 175 | 3/14/2023 |
| 1.1.0 | 750 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 183 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 186 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 176 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 181 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 182 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 177 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 175 | 3/14/2023 |
| 1.0.6 | 1,119 | 3/5/2022 |
| 1.0.4 | 1,161 | 6/13/2020 |
| 1.0.1 | 1,171 | 2/20/2020 |