RuoVea.ExJwtBearer 6.0.11.4

dotnet add package RuoVea.ExJwtBearer --version 6.0.11.4
NuGet\Install-Package RuoVea.ExJwtBearer -Version 6.0.11.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.11.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.11.4
#r "nuget: RuoVea.ExJwtBearer, 6.0.11.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.11.4

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

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

Package Downloads
RuoVea.OAuthServer

OAuth2.0 授权中心

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.11.4 138 3/13/2024
6.0.11.3 89 3/11/2024
6.0.11.2 89 2/27/2024
6.0.11.1 82 2/22/2024
6.0.11 419 9/8/2022
6.0.10 387 6/10/2022
6.0.9 386 3/25/2022
6.0.8 387 3/25/2022
6.0.7 390 3/25/2022
6.0.6 387 3/24/2022
6.0.5 366 3/24/2022
6.0.4 366 3/24/2022
6.0.3 373 3/24/2022
6.0.2 384 3/23/2022
6.0.1 370 3/22/2022
6.0.0 401 2/18/2022
5.0.1 391 3/23/2022
5.0.0 409 3/22/2022