Jc.AuthorizationFilter.JWT 1.0.5

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

// Install Jc.AuthorizationFilter.JWT as a Cake Tool
#tool nuget:?package=Jc.AuthorizationFilter.JWT&version=1.0.5                

Jc.AuthorizationFilter.JWT

基于Jwt的token验证模块

使用方法

全局过滤
Services.AddMvc(config=>
    {
        config.Filters.Add(new Jc.AuthorizationFilter.JWT(
            "token",
             TokenFrom.Query,
            "192.168.1.83:6379,password=passwrd"));
    }
);
参数说明:

1.支持多个重载,可以选择启用|不启用redis

如果不启用Redis,token的长度会根据 tokeninfo的内容而增加

2.设置白名单 token,action,controller等

名称 必选 可空 说明
tokenName token的字段名
tokenFrom token的来源(Query|Hearder)
freeControllers 控制器白名单
freeActions 方法名白名单
freeTokens 自定义的白名单Token
redisConn redis连接字符串,空:不启用redis,连接失败也不启用redis
    /// <summary>
    /// Token过滤器
    /// </summary>
    /// <param name="tokenName">token字段名称</param>
    /// <param name="tokenFrom">token来源(Hearder|Query)</param>
    /// <param name="freeControllers">放行的Controller</param>
    /// <param name="freeActions">放行的Action</param>
    /// <param name="freeTokens">放行的token白名单</param>
    /// <param name="redisConn">Redis链接</param>
    public JWT(
        string tokenName, 
        TokenFrom tokenFrom,
        List<string>? freeControllers, 
        List<string>? freeActions,
        List<string>? freeTokens,
        string redisConn)
指定Controller作用
[JWT("token",TokenFrom.Query)]
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{

}
指定Action作用
[JWT("token",TokenFrom.Query)]
[HttpPost("/test/checktoken")]
public IActionResult checktoken(string  token)
{

}
创建Token
/// <summary>
/// 创建Token
/// </summary>
/// <param name="seconds">超时时间(秒)</param>
/// <param name="tokenid">tokenId</param>
/// <param name="tokenInfo">tokenInfo</param>
/// <param name="secrityKey">secret</param>
/// <returns></returns>
public static string Create(
    int seconds, 
    string? tokenid,
    string? tokenInfo,
    string secrityKey = "53f8a45f-6891-4387-a769-1c29b482bf21")

tokenId:必须是唯一的 可以是用户的UUID等

tokenInfo:常用的常量信息 可以放在 tokenInfo中 比如 角色,姓名,等 可以在任何action中手动调取,不用频繁读库了

手动获取token信息
    /// <summary>
    /// 检查Token
    /// </summary>
    /// <param name="thisToken"></param>
    /// <returns></returns>
    public static (int Code, string? Msg, TokenInfo? Data) CheckToken(
        string? thisToken)
Code Msg Data explain
0 success TokenInfo Token信息
202 No identity information null 没有身份信息
203 Token verification failed: null token验证失败
204 Token time out null token超时
在Action中 获取TokenInfo

注:一定要在Action入口处使用 否则 在其他过程使用 可能会错乱ID

//方法1
TokenHelper.GetTokenInfo(Request.RouteValues);

//方法2
Request.RouteValues["tokenInfoObj"];
Product 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. 
.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.

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.2.18 115 8/30/2024
2.2.17 167 8/21/2024
2.2.16 142 8/20/2024
2.2.15 137 8/19/2024
2.2.14 135 8/16/2024
2.2.13 125 8/16/2024
1.2.10 129 6/5/2024
1.2.9 903 10/19/2023
1.2.8 807 9/12/2023
1.2.7 882 9/5/2023
1.2.6 813 9/5/2023
1.2.5 762 9/5/2023
1.0.5 1,111 3/7/2023
1.0.4 1,406 3/25/2022
1.0.3 1,349 3/22/2022
1.0.2 1,368 3/21/2022
1.0.1 1,393 3/19/2022