Lzq.Extensions.Jwt
0.2.0
dotnet add package Lzq.Extensions.Jwt --version 0.2.0
NuGet\Install-Package Lzq.Extensions.Jwt -Version 0.2.0
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="Lzq.Extensions.Jwt" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lzq.Extensions.Jwt" Version="0.2.0" />
<PackageReference Include="Lzq.Extensions.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 Lzq.Extensions.Jwt --version 0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Lzq.Extensions.Jwt, 0.2.0"
#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 Lzq.Extensions.Jwt@0.2.0
#: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=Lzq.Extensions.Jwt&version=0.2.0
#tool nuget:?package=Lzq.Extensions.Jwt&version=0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Lzq.Extensions.Jwt
基于 ASP.NET Core 的 JWT 认证扩展库,简化 Token 签发与用户身份读取。
快速上手
1. 配置 appsettings.json
json
{
"Jwt": {
"Issuer": "your-app",
"Audience": "your-client",
"SecurityKey": "your-secret-key-at-least-16-chars"
}
}
2. 注册服务
csharp
builder.Services.AddLzqJwt(builder.Configuration, options =>
{
options.Issuer = "your-app";
options.Audience = "your-app";
options.SecurityKey = "your-secret-key-at-least-16-chars";
});
app.UseAuthentication();
app.UseAuthorization();
3. 生成 Token
csharp
public class AuthService
{
private readonly IJwtService _jwtService;
public AuthService(IJwtService jwtService) => _jwtService = jwtService;
public TokenViewDto Login()
{
var user = new CurrentUser()
.SetUserId("001")
.SetUserName("demo")
.SetRoles(new() { "admin" });
return _jwtService.GenerateToken(user, TimeSpan.FromHours(2));
}
}
4. 获取当前用户
csharp
[Authorize]
public class UserController : ControllerBase
{
private readonly ICurrentUser _user;
public UserController(ICurrentUser user) => _user = user;
[HttpGet]
public IActionResult Get() => Ok(new { _user.UserId, _user.UserName });
}
主要接口
| 接口/类 | 说明 |
|---|---|
IJwtService |
生成 JWT Token |
ICurrentUser |
获取当前请求用户信息(UserId、Roles、Email 等) |
JwtOption |
配置选项,对应配置文件 Jwt 节 |
依赖项
Microsoft.AspNetCore.Authentication.JwtBearerMasa.Utils.Security.Token
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 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.
-
net10.0
- Lzq.Core (>= 0.2.0)
- Masa.Utils.Security.Token (>= 1.2.0-preview.10)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.8)
-
net8.0
- Lzq.Core (>= 0.2.0)
- Masa.Utils.Security.Token (>= 1.2.0-preview.10)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.27)
-
net9.0
- Lzq.Core (>= 0.2.0)
- Masa.Utils.Security.Token (>= 1.2.0-preview.10)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.16)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.