RuoVea.ExJwtBearer 7.0.0.4

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

// Install RuoVea.ExJwtBearer as a Cake Tool
#tool nuget:?package=RuoVea.ExJwtBearer&version=7.0.0.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 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. 
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.Auth

用户登陆 API

RuoVea.OmiApi.User

用户管理 API

RuoVea.OmiApi.SystemApp

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

RuoVea.OmiApi.UserRole

用户角色管理 API

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0.4 82 10/10/2024
8.0.0.3 78 10/10/2024
8.0.0.2 85 9/22/2024
8.0.0.1 103 9/19/2024
8.0.0 111 8/29/2024
7.0.0.4 72 10/10/2024
7.0.0.3 81 10/10/2024
7.0.0.2 82 9/22/2024
7.0.0.1 84 9/19/2024
7.0.0 95 8/29/2024
6.0.12.4 1,065 10/10/2024
6.0.12.3 97 10/10/2024
6.0.12.2 288 9/22/2024
6.0.12.1 91 9/19/2024
6.0.12 87 8/29/2024
6.0.11.4 216 3/13/2024
6.0.11.3 126 3/11/2024
6.0.11.2 120 2/27/2024
6.0.11.1 108 2/22/2024
6.0.11 451 9/8/2022
6.0.10 415 6/10/2022
6.0.9 410 3/25/2022
6.0.8 396 3/25/2022
6.0.7 421 3/25/2022
6.0.6 422 3/24/2022
6.0.5 391 3/24/2022
6.0.4 403 3/24/2022
6.0.3 388 3/24/2022
6.0.2 416 3/23/2022
6.0.1 380 3/22/2022
6.0.0 431 2/18/2022
5.0.1.1 74 10/10/2024
5.0.1 417 3/23/2022
5.0.0 434 3/22/2022