Lzq.Extensions.Jwt 0.0.8

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

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.JwtBearer
  • Masa.Utils.Security.Token
Product 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.

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
0.2.0 32 5/19/2026
0.0.9 42 5/17/2026
0.0.8 43 5/17/2026
0.0.5 39 5/17/2026