mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-25 03:06:36 +08:00
大版本更新
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -15,51 +17,30 @@ namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
public class CustomAuthorizationHandler : AuthorizationHandler<CustomAuthorizationRequirement>
|
||||
{
|
||||
|
||||
private CacheClientDB _cacheClientDB;
|
||||
//private CacheClientDB _cacheClientDB;
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public CustomAuthorizationHandler(CacheClientDB cacheClientDB)
|
||||
public CustomAuthorizationHandler()
|
||||
{
|
||||
_cacheClientDB= cacheClientDB;
|
||||
}
|
||||
|
||||
//验证的方法就在这里
|
||||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomAuthorizationRequirement requirement)
|
||||
{
|
||||
var currentClaim = context.User.Claims.FirstOrDefault(u => u.Type == ClaimTypes.Sid);
|
||||
|
||||
if (currentClaim==null) //说明没有写入Sid 没有登录
|
||||
{
|
||||
return Task.CompletedTask; //验证不同过
|
||||
}
|
||||
|
||||
int currentUserId = 0;
|
||||
if (!string.IsNullOrWhiteSpace(currentClaim.Value))
|
||||
{
|
||||
currentUserId = Convert.ToInt32(currentClaim.Value);
|
||||
}
|
||||
DefaultHttpContext httpcontext = (DefaultHttpContext)context.Resource;
|
||||
Dictionary<string, string> dicMenueDictionary = new Dictionary<string, string>();
|
||||
//现在只需要登录的时候把用户的api路径添加到redis去
|
||||
//每次访问的时候进行redis判断一下即可
|
||||
//注意一下,redis不能一直保存,和jwt一样搞一个期限
|
||||
//var menuList=_cacheClientDB.Get<List<menuDto>>(RedisConst.userMenusApi+":"+currentUserId);
|
||||
//foreach (var k in menuList)
|
||||
//{
|
||||
// if (k.mould != null)
|
||||
// {
|
||||
// dicMenueDictionary.Add(k.mould?.id.ToString(), "/api"+ k.mould?.url);
|
||||
// }
|
||||
|
||||
//}
|
||||
|
||||
if (dicMenueDictionary.ContainsValue(httpcontext.Request.Path))
|
||||
var currentClaim = context.User.Claims.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid);
|
||||
//DefaultHttpContext httpcontext = (DefaultHttpContext)context.AuthenticateAsync();
|
||||
if (currentClaim!=null) //说明没有写入Sid 没有登录
|
||||
{
|
||||
context.Succeed(requirement); //验证通过了
|
||||
}
|
||||
//string currentUserId = "";
|
||||
//if (!string.IsNullOrWhiteSpace(currentClaim.Value))
|
||||
//{
|
||||
// currentUserId = currentClaim.Value;
|
||||
//}
|
||||
//DefaultHttpContext httpcontext = (DefaultHttpContext)context.Resource;
|
||||
return Task.CompletedTask; //验证不同过
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,6 @@ namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
}
|
||||
public static class PolicyName
|
||||
{
|
||||
public const string Menu = "Menu";
|
||||
public const string Sid = "Sid";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user