RuoVea.ExJwtBearer
8.0.0.5
dotnet add package RuoVea.ExJwtBearer --version 8.0.0.5
NuGet\Install-Package RuoVea.ExJwtBearer -Version 8.0.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="RuoVea.ExJwtBearer" Version="8.0.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RuoVea.ExJwtBearer" Version="8.0.0.5" />
<PackageReference Include="RuoVea.ExJwtBearer" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RuoVea.ExJwtBearer --version 8.0.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RuoVea.ExJwtBearer, 8.0.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.
#addin nuget:?package=RuoVea.ExJwtBearer&version=8.0.0.5
#tool nuget:?package=RuoVea.ExJwtBearer&version=8.0.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RuoVea.ExJwtBearer
介绍
JWT扩展帮助类库,支持Jwt 鉴权、Basic 认证、IdentityServer4 Client认证、Authing认证。支持简体中文、繁体中文、粤语、日语、法语、英语.
1.快速入门
通过Nuget安装组件
Install-Package RuoVea.ExJwtBearer
实现当前用户信息类:AspNetUser继承自ICurrentUser
2、使用示例
2.1、注入 Jwt
添加验权
services.AddAuthenticationSetup(IdentifyEnum.);/*添加验权*/
添加鉴权
services.AddAuthorizationSetup<MyPermission>();/*添加鉴权*/
添加JWT加密
services.AddJwtSetup();/*添加JWT加密*/
app添加
app.UseAuthentication();
app.UseAuthorization();
globalAuthorize=false时候可以用验证:[Authorize]、[BearerAuthorize]、
[Authorize(Roles="admin")] 可以用角色进行多角色用户验证使用
2.2、使用Jwt
声名 IJwtHelper _jwtHelper
_jwtHelper.Encrypt(new Dictionary<string, object>)
3、登录时使用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;
4、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秒
}
}
5、basic 认证配置文件
{
/* Basic认证配置 */
"BasicAuth": {
"ShowFrom": true, // 是否触发弹窗显示
"users":[
{"username":"admin","password":"admin","role":"admin"}
]
}
}
6、IdentityServer4 Client认证配置文件
{
/* IdentityServer4 Client*/
"Ids4Client": {
"AuthorizationUrl": "http://localhost:5004", /*认证中心域名*/
"ApiName": "" /*资源服务器*/
}
}
7、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);
}
}
8、常量、公共方法配置类
/// <summary>
/// 常量、公共方法配置类
/// </summary>
public static class Permissions
{
/// <summary>
/// 授权策略前缀
/// </summary>
public const string Name = "Permission";
}
9、总是遇到401问题添加如下代码
app.UseAuthentication();
app.UseAuthorization();
10、JWT 加解密接口方法(IJwtHelper)
/// <summary>
/// 创建登录时候的Token
/// </summary>
/// <param name="user">登录时用户信息</param>
/// <param name="extend">扩展信息:可以用来补充user中不够的数据,扩展中的数据优先,若要支持数组,选用Dictionary模式传入数组</param>
/// <param name="expiredTime">过期时间(分钟)默认配置文件中的1440分钟(24小时)</param>
/// <returns></returns>
string CreateToken(UserVo user, List<Claim> extend = null, long? expiredTime = null);
/// <summary>
/// 登录时创建Token使用
/// </summary>
/// <param name="user">登录时用户信息</param>
/// <param name="extend">扩展信息:可以用来补充user中不够的数据,扩展中的数据优先</param>
/// <param name="expiredTime">过期时间(分钟)默认20分钟</param>
/// <returns></returns>
string TokenBuilder(UserVo user, Dictionary<string, object> extend=null, long? expiredTime = null);
/// <summary>
/// 生成 Token
/// </summary>
/// <param name="payload"></param>
/// <param name="expiredTime">过期时间(分钟)</param>
/// <returns></returns>
string Encrypt(IDictionary<string, object> payload, long? expiredTime = null);
/// <summary>
/// 生成 Token
/// </summary>
/// <param name="issuerSigningKey"></param>
/// <param name="payload"></param>
/// <param name="algorithm"></param>
/// <returns></returns>
string Encrypt(string issuerSigningKey, IDictionary<string, object> payload, string algorithm = SecurityAlgorithms.HmacSha256);
/// <summary>
/// 生成 Token
/// </summary>
/// <param name="issuerSigningKey"></param>
/// <param name="payload"></param>
/// <param name="algorithm"></param>
/// <returns></returns>
string Encrypt(string issuerSigningKey, string payload, string algorithm = SecurityAlgorithms.HmacSha256);
/// <summary>
/// 生成刷新 Token
/// </summary>
/// <param name="accessToken"></param>
/// <param name="expiredTime">刷新 Token 有效期(分钟)</param>
/// <returns></returns>
string GenerateRefreshToken(string accessToken, int expiredTime = 43200);
/// <summary>
/// 自动刷新 Token 信息
/// </summary>
/// <param name="context"></param>
/// <param name="httpContext"></param>
/// <param name="expiredTime">新 Token 过期时间(分钟)</param>
/// <param name="refreshTokenExpiredTime">新刷新 Token 有效期(分钟)</param>
/// <param name="tokenPrefix"></param>
/// <param name="clockSkew"></param>
/// <returns></returns>
Task<bool> AutoRefreshTokenAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext, long? expiredTime = null, int refreshTokenExpiredTime = 43200, string tokenPrefix = "Bearer ", long clockSkew = 5);
/// <summary>
/// 验证 Token
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
Task<(bool IsValid, JsonWebToken Token, TokenValidationResult validationResult)> ValidateAsync(string accessToken);
/// <summary>
/// 通过过期Token 和 刷新Token 换取新的 Token
/// </summary>
/// <param name="expiredToken"></param>
/// <param name="refreshToken"></param>
/// <param name="expiredTime">过期时间(分钟)</param>
/// <param name="clockSkew">刷新token容差值,秒做单位</param>
/// <returns></returns>
Task<string> ExchangeAsync(string expiredToken, string refreshToken, long? expiredTime = null, long clockSkew = 5);
/// <summary>
/// 验证 Token
/// </summary>
/// <param name="httpContext"></param>
/// <param name="headerKey"></param>
/// <param name="tokenPrefix"></param>
/// <returns></returns>
Task<(bool IsValid, JsonWebToken token)> ValidateJwtBearerTokenAsync(DefaultHttpContext httpContext, string headerKey = "Authorization", string tokenPrefix = "Bearer ");
/// <summary>
/// 读取 Token,不含验证
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
JsonWebToken ReadJwtToken(string accessToken);
/// <summary>
/// 获取 JWT Bearer Token
/// </summary>
/// <param name="httpContext"></param>
/// <param name="headerKey"></param>
/// <param name="tokenPrefix"></param>
/// <returns></returns>
string GetJwtBearerToken(DefaultHttpContext httpContext, string headerKey = "Authorization", string tokenPrefix = "Bearer ");
/// <summary>
/// 生成Token验证参数
/// </summary>
/// <param name="jwtSettings"></param>
/// <returns></returns>
TokenValidationParameters CreateTokenValidationParameters(JwtConfig jwtSettings);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.8)
- NetDevPack.Security.JwtExtensions (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RuoVea.ExConfig (>= 8.0.0.1)
- RuoVea.ExDto (>= 8.0.1.1)
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.UserRole
用户角色管理 API |
|
RuoVea.OmiApi.User
用户管理 API |
|
RuoVea.OmiApi.Auth
用户登陆 API |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.0.5 | 269 | 4/23/2025 |
8.0.0.4 | 567 | 10/10/2024 |
8.0.0.3 | 112 | 10/10/2024 |
8.0.0.2 | 117 | 9/22/2024 |
8.0.0.1 | 144 | 9/19/2024 |
8.0.0 | 138 | 8/29/2024 |
7.0.0.5 | 260 | 4/23/2025 |
7.0.0.4 | 604 | 10/10/2024 |
7.0.0.3 | 113 | 10/10/2024 |
7.0.0.2 | 113 | 9/22/2024 |
7.0.0.1 | 112 | 9/19/2024 |
7.0.0 | 129 | 8/29/2024 |
6.0.12.5 | 318 | 4/23/2025 |
6.0.12.4 | 2,879 | 10/10/2024 |
6.0.12.3 | 129 | 10/10/2024 |
6.0.12.2 | 323 | 9/22/2024 |
6.0.12.1 | 124 | 9/19/2024 |
6.0.12 | 115 | 8/29/2024 |
6.0.11.4 | 239 | 3/13/2024 |
6.0.11.3 | 151 | 3/11/2024 |
6.0.11.2 | 146 | 2/27/2024 |
6.0.11.1 | 135 | 2/22/2024 |
6.0.11 | 484 | 9/8/2022 |
6.0.10 | 449 | 6/10/2022 |
6.0.9 | 449 | 3/25/2022 |
6.0.8 | 435 | 3/25/2022 |
6.0.7 | 461 | 3/25/2022 |
6.0.6 | 460 | 3/24/2022 |
6.0.5 | 431 | 3/24/2022 |
6.0.4 | 444 | 3/24/2022 |
6.0.3 | 428 | 3/24/2022 |
6.0.2 | 456 | 3/23/2022 |
6.0.1 | 421 | 3/22/2022 |
6.0.0 | 475 | 2/18/2022 |
5.0.1.2 | 151 | 4/23/2025 |
5.0.1.1 | 108 | 10/10/2024 |
5.0.1 | 458 | 3/23/2022 |
5.0.0 | 474 | 3/22/2022 |