HMENetCore.Jwt
6.0.52
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package HMENetCore.Jwt --version 6.0.52
NuGet\Install-Package HMENetCore.Jwt -Version 6.0.52
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="HMENetCore.Jwt" Version="6.0.52" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.Jwt" Version="6.0.52" />
<PackageReference Include="HMENetCore.Jwt" />
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 HMENetCore.Jwt --version 6.0.52
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HMENetCore.Jwt, 6.0.52"
#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 HMENetCore.Jwt@6.0.52
#: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=HMENetCore.Jwt&version=6.0.52
#tool nuget:?package=HMENetCore.Jwt&version=6.0.52
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HMENetCore.Jwt
简介
HMENetCore.Jwt
是一个为 ASP.NET Core 应用提供 JWT 认证相关功能的扩展库,包含 JWT 令牌生成、解析、验证以及 Swagger 集成和鉴权等功能。
功能特性
- JWT 令牌生成:支持自定义 claims 和多种配置方式
- JWT 令牌解析:从令牌中提取 payload 数据
- JWT 认证中间件:简化 JWT 认证配置
- Swagger 集成:自动添加 JWT 认证支持到 Swagger UI
- 多方式获取令牌:支持从 Header、Query、Form 等多种方式获取令牌
运行环境
- 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.
安装
dotnet add package HMENetCore.Jwt
使用说明
1. JWT 配置
appsettings.json配置:
{
"JWT": {
"Issuer": "your_issuer",
"AccessTokenAudience": "your_audience",
"SecurityKey": "your_security_key_at_least_16_chars",
"AccessTokenExpires": 120,
"AuthorizationKey": "Authorization"
}
}
2. 配置 JWT 认证
// 使用默认配置
builder.Services.AddJWTAuthentications(Configuration.GetSection("JWT").Get<JWTConfig>());
// 或者使用自定义配置
builder.Services.AddJWTAuthentications(options => {
options.Issuer = "custom_issuer";
options.AccessTokenAudience = "custom_audience";
options.SecurityKey = "custom_security_key";
options.AccessTokenExpires = 60;
});
3. 生成 JWT 令牌
public class AuthController : ControllerBase
{
private readonly JWTConfig _jwtConfig;
public AuthController(IConfiguration configuration)
{
_jwtConfig = configuration.GetSection("JWT").Get<JWTConfig>();
}
[HttpPost("login")]
public async Task<IActionResult> Login([FromBody] LoginModel model)
{
// 验证用户逻辑...
var tokenModel = new JwtTokenModel {
UserId = user.Id,
UserName = user.UserName,
Role = user.Role
};
var token = await JwtTokenHelper.CreateToken(tokenModel, _jwtConfig);
return Ok(new {
token = token.accressToken,
expires = token.expiresTime
});
}
}
4. 解析 JWT 令牌
[HttpGet("profile")]
[Authorize]
public IActionResult GetProfile()
{
// 从请求中获取令牌
var token = HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
// 解析令牌
var userInfo = JwtTokenHelper.GetToken<JwtTokenModel>(token);
return Ok(userInfo);
}
5. Swagger 集成
// 配置 Swagger
builder.Services.AddSwaggerGen(c =>
{
c.AddAuthenticationHeader("v1", new OpenApiInfo()
{
Title = "My API",
Description = "",
Version = "v1"
});
SwaggerGenExtensions.IncludeXmlComments(c);
});
// 在中间件中启用 Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.SwaggerUISpanEditable(); // 可选:使 Swagger UI 中的 span 可编辑
});
6. 自定义认证事件
var events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
{
context.Response.Headers.Add("Token-Expired", "true");
}
return Task.CompletedTask;
},
OnChallenge = context =>
{
context.HandleResponse();
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
context.Response.ContentType = "application/json";
var result = new { Code = 401, Message = "Unauthorized", Result = "" };
var json = JsonSerializer.Serialize(result);
return context.Response.WriteAsync(json);
},
OnMessageReceived = context =>
{
// 自定义令牌获取逻辑
if (context.Request.Query.TryGetValue("token", out var token))
{
context.Token = token;
}
return Task.CompletedTask;
}
};
// 注册服务时使用自定义事件
builder.Services.AddJWTAuthentications(_jwtConfig, events: events);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 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.
-
net6.0
- IGeekFan.AspNetCore.Knife4jUI (>= 0.0.16)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 6.0.36)
- Swashbuckle.AspNetCore (>= 7.3.2)
- Swashbuckle.AspNetCore.Filters (>= 9.0.0)
-
net8.0
- IGeekFan.AspNetCore.Knife4jUI (>= 0.0.16)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.19)
- Swashbuckle.AspNetCore (>= 9.0.3)
- Swashbuckle.AspNetCore.Filters (>= 9.0.0)
-
net9.0
- IGeekFan.AspNetCore.Knife4jUI (>= 0.0.16)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.8)
- Swashbuckle.AspNetCore (>= 9.0.3)
- Swashbuckle.AspNetCore.Filters (>= 9.0.0)
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 |
---|---|---|
6.0.53 | 138 | 8/13/2025 |
6.0.52 | 138 | 8/13/2025 |
6.0.51 | 130 | 8/11/2025 |
6.0.50 | 131 | 8/11/2025 |
6.0.49 | 215 | 8/7/2025 |
6.0.48 | 127 | 7/16/2025 |
6.0.47 | 152 | 7/8/2025 |
6.0.46 | 148 | 7/2/2025 |
6.0.45 | 160 | 6/18/2025 |
6.0.43 | 260 | 6/13/2025 |
6.0.43-preview.1 | 254 | 6/10/2025 |
6.0.42 | 273 | 6/10/2025 |
6.0.41 | 157 | 6/3/2025 |
6.0.39 | 235 | 5/15/2025 |
6.0.38 | 160 | 5/7/2025 |
6.0.37 | 160 | 4/27/2025 |
6.0.35 | 187 | 4/23/2025 |
6.0.31 | 167 | 3/17/2025 |
6.0.30 | 132 | 2/19/2025 |
6.0.20 | 140 | 2/5/2025 |
6.0.12 | 139 | 12/9/2024 |
6.0.11 | 118 | 11/27/2024 |