RuoVea.ExJwtBearer 6.0.12.4

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.4                
NuGet\Install-Package RuoVea.ExJwtBearer -Version 6.0.12.4                
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.4" />                
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.4                
#r "nuget: RuoVea.ExJwtBearer, 6.0.12.4"                
#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.4

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

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.User

用户管理 API

RuoVea.OmiApi.UserRoleMenu

用户、角色、菜单管理 API

RuoVea.OmiApi.Auth

用户登陆 API

RuoVea.OmiApi.UserRole

用户角色管理 API

RuoVea.OmiApi.SystemApp

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

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0.4 89 10/10/2024
8.0.0.3 83 10/10/2024
8.0.0.2 88 9/22/2024
8.0.0.1 106 9/19/2024
8.0.0 114 8/29/2024
7.0.0.4 77 10/10/2024
7.0.0.3 86 10/10/2024
7.0.0.2 85 9/22/2024
7.0.0.1 87 9/19/2024
7.0.0 98 8/29/2024
6.0.12.4 1,218 10/10/2024
6.0.12.3 102 10/10/2024
6.0.12.2 291 9/22/2024
6.0.12.1 95 9/19/2024
6.0.12 90 8/29/2024
6.0.11.4 219 3/13/2024
6.0.11.3 129 3/11/2024
6.0.11.2 123 2/27/2024
6.0.11.1 111 2/22/2024
6.0.11 454 9/8/2022
6.0.10 418 6/10/2022
6.0.9 413 3/25/2022
6.0.8 399 3/25/2022
6.0.7 424 3/25/2022
6.0.6 425 3/24/2022
6.0.5 394 3/24/2022
6.0.4 406 3/24/2022
6.0.3 391 3/24/2022
6.0.2 419 3/23/2022
6.0.1 383 3/22/2022
6.0.0 434 2/18/2022
5.0.1.1 79 10/10/2024
5.0.1 420 3/23/2022
5.0.0 437 3/22/2022