RuoVea.ExJwtBearer
6.0.11.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package RuoVea.ExJwtBearer --version 6.0.11.1
NuGet\Install-Package RuoVea.ExJwtBearer -Version 6.0.11.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.11.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.11.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RuoVea.ExJwtBearer, 6.0.11.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.11.1 // Install RuoVea.ExJwtBearer as a Cake Tool #tool nuget:?package=RuoVea.ExJwtBearer&version=6.0.11.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RuoVea.ExJwtBearer
JWT扩展帮助类库
使用示例
注入 Jwt
添加Jwt验证
services.AddJwtAuthorization(enableGlobalAuthorize: true);
添加JWT及鉴权
services.AddJwtAuthorization<JwtHandler>(enableGlobalAuthorize: true);
添加JWT加密
services.AddJwtEncryption();
app添加
app.UseAuthentication();
app.UseAuthorization();
enableGlobalAuthorize=false时候可以只有验证;[Authorize(AuthenticationSchemes = "Bearer")]、[BearerAuthorize]
使用Jwt
声名 IJwtToken _jwtToken
_jwtToken.Encrypt(new Dictionary<string, object>)
登录时使用Jwt
使用方式一、
// 生成Token令牌
var accessToken = LoginToken(UserVo user, Dictionary<string, object> extend);
// 设置Swagger自动登录
_httpContextAccessor.HttpContext.SigninToSwagger(accessToken);
// 生成刷新Token令牌
//var refreshToken = _jwtToken.GenerateRefreshToken(accessToken, 30);
// 设置刷新Token令牌
//_httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
使用方式二、
// 生成Token令牌
var accessToken = _jwtToken.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 = _jwtToken.GenerateRefreshToken(accessToken, 30);
// 设置刷新Token令牌
//_httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
配置文件
{
/* Jwt配置 */
"JWTSettings": {
"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秒
}
}
AppAuthorizeHandler 实现类
/// <summary>
/// 实现
/// </summary>
public class JwtHandler : AppAuthorizeHandler
{
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PolicyRequirement requirement)
{
//// 自动刷新 token
//if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext()))
//{
// await AuthorizeHandleAsync(context);
//}
// 判断是否授权
var isAuthenticated = context.User.Identity.IsAuthenticated;
if (isAuthenticated)
{
await AuthorizeHandleAsync(context);
}
else context.GetCurrentHttpContext()?.SignoutToSwagger(); // 退出Swagger登录
}
/// <summary>
/// 授权处理
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected async Task AuthorizeHandleAsync(AuthorizationHandlerContext context)
{
// 获取所有未成功验证的需求
var pendingRequirements = context.PendingRequirements;
// 获取 HttpContext 上下文
var httpContext = context.GetCurrentHttpContext();
// 调用子类管道
// 此处已经自动验证 Jwt Token的有效性了,无需手动验证
var pipeline = await CheckAuthorzieAsync(httpContext);
if (pipeline)
{
Task.FromResult(true);
// 通过授权验证
foreach (var requirement in pendingRequirements)
{
// 验证策略管道
var policyPipeline = await PolicyPipelineAsync(context, httpContext, requirement);
if (policyPipeline) context.Succeed(requirement);
}
}
else context.Fail();
}
/// <summary>
/// 检查权限
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
private static async Task<bool> CheckAuthorzieAsync(DefaultHttpContext httpContext)
{
// 管理员跳过判断
var userManager = App.GetService<IUserManager>();
if (userManager.SuperAdmin) return true;
// 路由名称
var routeName = httpContext.Request.Path.Value.Substring(1).Replace("/", ":");
var allPermission = await App.GetService<ISysMenuService>().GetAllPermission();
if (!allPermission.Contains(routeName))
{
return true;
}
// 默认路由(获取登录用户信息)
var defalutRoute = new List<string>()
{
"Auth:getLoginUser",
"getLoginUser",
"sysNotice:unread",
"codeGenerate:InformationList",
"sysFileInfo:uploadAvatar",
"sysFileInfo:preview"
};
if (defalutRoute.Contains(routeName)) return true;
// 获取用户权限集合(按钮或API接口)
var permissionList = await App.GetService<ISysMenuService>().GetLoginPermissionList(userManager.UserId);
// 检查授权
return permissionList.Contains(routeName);
}
/// <summary>
/// 策略验证管道
/// </summary>
/// <param name="context"></param>
/// <param name="httpContext"></param>
/// <param name="requirement"></param>
/// <returns></returns>
public virtual Task<bool> PolicyPipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext, IAuthorizationRequirement requirement)
{
return Task.FromResult(true);
}
}
总是遇到401问题添加如下代码
app.UseAuthentication();
app.UseAuthorization();
Product | Versions 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.
-
net6.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 6.0.3)
- RuoVea.ExConfig (>= 6.0.1.3)
- RuoVea.ExDto (>= 6.0.11)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on RuoVea.ExJwtBearer:
Package | Downloads |
---|---|
RuoVea.OmiApi.UserRoleMenu
用户、角色、菜单管理 API |
|
RuoVea.OmiApi.User
用户管理 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,287 | 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 |