RuoVea.ExJwtBearer 6.0.12.1

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

// Install RuoVea.ExJwtBearer as a Cake Tool
#tool nuget:?package=RuoVea.ExJwtBearer&version=6.0.12.1                

RuoVea.ExJwtBearer

JWT扩展帮助类库,支持简体中文、繁体中文、粤语、日语、法语、英语.

使用示例

注入 Jwt

添加验权

services.AddAuthenticationSetup(IdentifyEnum.);/*添加验权*/

添加鉴权

services.AddAuthorizationSetup<MyPermission>();/*添加鉴权*/

添加JWT加密

services.AddJwtSetup();/*添加JWT加密*/

app添加

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

globalAuthorize=false时候可以用验证:[Authorize]、[BearerAuthorize]、

[Authorize(Roles="admin")] 可以用角色进行多角色用户验证使用

使用Jwt

声名 IJwtHelper _jwtHelper

_jwtHelper.Encrypt(new Dictionary<string, object>)
登录时使用Jwt

使用方式一、

     // 生成Token令牌
     var accessToken =  _jwtHelper.CreateToken(UserVo user, Dictionary<string, object> extend);
    
     // 设置Swagger自动登录
     _httpContextAccessor.HttpContext.SigninToSwagger(accessToken);
    
     // 生成刷新Token令牌
     //var refreshToken = _jwtHelper.GenerateRefreshToken(accessToken, 30);
    
     // 设置刷新Token令牌
     //_httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;


使用方式二、

     // 生成Token令牌
     var accessToken = _jwtHelper.Encrypt(new Dictionary<string, object>
     {
         {ClaimConst.CLAINM_USERID, user.Id},
         {ClaimConst.TENANT_ID, user.TenantId},
         {ClaimConst.CLAINM_ACCOUNT, user.Account},
         {ClaimConst.CLAINM_NAME, user.Name},
         {ClaimConst.CLAINM_SUPERADMIN, user.AdminType},
     });
    
     // 设置Swagger自动登录
     _httpContextAccessor.HttpContext.SigninToSwagger(accessToken);
    
     // 生成刷新Token令牌
     //var refreshToken = _jwtHelper.GenerateRefreshToken(accessToken, 30);
    
     // 设置刷新Token令牌
     //_httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
Jwt 配置文件
{
  /* Jwt配置 */
  "Jwt": {
    "ValidateIssuerSigningKey": true, // 是否验证密钥,bool 类型,默认true
    "IssuerSigningKey": "3c1cbc3f546eda35168c3aa3cb91780fbe703f0996c6d123ea96dc85c70bbc0a", // 密钥,string 类型,必须是复杂密钥,长度大于16
    "ValidateIssuer": true, // 是否验证签发方,bool 类型,默认true
    "ValidIssuer": "SecurityDemo.Authentication.JWT", // 签发方,string 类型
    "ValidateAudience": true, // 是否验证签收方,bool 类型,默认true
    "ValidAudience": "jwtAudience", // 签收方,string 类型
    "ValidateLifetime": true, // 是否验证过期时间,bool 类型,默认true,建议true
    "ExpiredTime": 1440, // 过期时间,long 类型,单位分钟,默认1440分钟(24小时)
    "ClockSkew": 5 // 过期时间容错值,long 类型,单位秒,默认5秒
  }
}
basic 认证配置文件
{
  /* Basic认证配置 */
  "BasicAuth": {
    "ShowFrom": true, // 是否触发弹窗显示
    "users":[
        {"username":"admin","password":"admin","role":"admin"}
    ]
  }
}
IdentityServer4 Client认证配置文件
{
  /* IdentityServer4 Client*/
  "Ids4Client": {
    "AuthorizationUrl": "http://localhost:5004", /*认证中心域名*/
    "ApiName": "" /*资源服务器*/
  }
}

AppAuthorizeHandler 实现类

/// <summary>
   /// 鉴权验证
   /// </summary>
   public class MyPermission : ApplicationPermission
   {
       /// <summary>
       /// 用户信息
       /// </summary>
       private readonly IUser _user;
       /// <summary>
       /// 
       /// </summary>
       /// <param name="user"></param>
       public MyPermission(IUser user)
       {
           _user = user;
       }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="httpContext"></param>
       /// <param name="permission">当前url地址冒号(:)形式</param>
       /// <returns></returns>
       public override async Task<bool> VerifyPermissionAsync(DefaultHttpContext httpContext, string permission)
       {
           long userId = _user.UserId;
           // .... 实现验证权限
           if(true) 
               return await Task.FromResult(true);
           else 
               return await Task.FromResult(false);
       }
   }

总是遇到401问题添加如下代码

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

Product 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on RuoVea.ExJwtBearer:

Package Downloads
RuoVea.OmiApi.UserRoleMenu

用户、角色、菜单管理 API

RuoVea.OmiApi.SystemApp

用户、角色、菜单、机构、职位、权限管理 API

RuoVea.OmiApi.User

用户管理 API

RuoVea.OmiApi.UserRole

用户角色管理 API

RuoVea.OmiApi.Auth

用户登陆 API

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0.4 94 10/10/2024
8.0.0.3 90 10/10/2024
8.0.0.2 96 9/22/2024
8.0.0.1 110 9/19/2024
8.0.0 118 8/29/2024
7.0.0.4 84 10/10/2024
7.0.0.3 93 10/10/2024
7.0.0.2 91 9/22/2024
7.0.0.1 91 9/19/2024
7.0.0 102 8/29/2024
6.0.12.4 1,702 10/10/2024
6.0.12.3 107 10/10/2024
6.0.12.2 299 9/22/2024
6.0.12.1 100 9/19/2024
6.0.12 94 8/29/2024
6.0.11.4 221 3/13/2024
6.0.11.3 131 3/11/2024
6.0.11.2 126 2/27/2024
6.0.11.1 113 2/22/2024
6.0.11 456 9/8/2022
6.0.10 420 6/10/2022
6.0.9 415 3/25/2022
6.0.8 401 3/25/2022
6.0.7 426 3/25/2022
6.0.6 427 3/24/2022
6.0.5 398 3/24/2022
6.0.4 408 3/24/2022
6.0.3 393 3/24/2022
6.0.2 421 3/23/2022
6.0.1 385 3/22/2022
6.0.0 436 2/18/2022
5.0.1.1 86 10/10/2024
5.0.1 422 3/23/2022
5.0.0 439 3/22/2022