Common.JwtToken 1.2.1

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Common.JwtToken --version 1.2.1                
NuGet\Install-Package Common.JwtToken -Version 1.2.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="Common.JwtToken" Version="1.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Common.JwtToken --version 1.2.1                
#r "nuget: Common.JwtToken, 1.2.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.
// Install Common.JwtToken as a Cake Addin
#addin nuget:?package=Common.JwtToken&version=1.2.1

// Install Common.JwtToken as a Cake Tool
#tool nuget:?package=Common.JwtToken&version=1.2.1                

jwtToken包

一个封装了jwt使用以及swagger启用等的包

快速上手

认证

注入jwt服务

services.AddJwtTokenService(options =>
{
    options.JwtAudience = "aaaa";
    options.JwtIssuer = "bbbb";
});

开启认证

app.UseAuthentication();
app.UseAuthorization();

使用服务IJwtAuthService

private readonly IJwtAuthService _jwtAuthService;
private readonly ILogger<TokenController> _logger;

public TokenController(IJwtAuthService jwtAuthService, ILogger<TokenController> logger)
{
    _jwtAuthService = jwtAuthService;
    _logger = logger;
}

/// <summary>
/// 获取token
/// </summary>
/// <returns></returns>
[HttpGet]
public string GetToken()
{
    var claim = new List<Claim>
    {
        new Claim(ClaimTypes.NameIdentifier,"111111"),
        new Claim(ClaimTypes.Role,"123456")
    };
    return _jwtAuthService.CreateToken(claim);
}

授权

注入服务

services.AddCustomerAuthorization("default");

实现授权获取接口,例如

public class PermissionService : IPermissionService
{
    public Task<List<RolePermissionRelationDto>> GetRoleAuthAsync()
    {
        return Task.FromResult(new List<RolePermissionRelationDto>()
        {
            new RolePermissionRelationDto("123456","/api/token/get")
        });
    }
}

注入

services.AddScoped<IPermissionService, PermissionService>();

在需要开启授权的Action上标注

[Authorize("default")]
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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 is compatible.  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. 
.NET Core netcoreapp3.1 is compatible. 
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
2.0.0-beta1 157 1/21/2024 2.0.0-beta1 is deprecated because it is no longer maintained.
1.2.1 327 3/18/2023 1.2.1 is deprecated because it is no longer maintained.
1.2.0-beta2 194 2/6/2023
1.2.0-beta1 167 11/14/2022
1.1.3 521 4/27/2022
1.1.2 8,081 12/15/2020
1.1.1 452 11/27/2020
1.1.0 456 11/11/2020
1.0.0 450 10/10/2020