其他分享
首页 > 其他分享> > EAuthorizationService

EAuthorizationService

作者:互联网

    public class EAuthorizationService :DefaultAuthorizationService,  IAuthorizationService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public EbkAuthorizationService(IAuthorizationPolicyProvider policyProvider, IAuthorizationHandlerProvider handlers, ILogger<DefaultAuthorizationService> logger, IAuthorizationHandlerContextFactory contextFactory, IAuthorizationEvaluator evaluator, IOptions<AuthorizationOptions> options, IHttpContextAccessor httpContextAccessor) : base(policyProvider, handlers, logger, contextFactory, evaluator, options)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public async new Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
        {
            Console.WriteLine("EAuthorizationService -- requirements");
            var header = _httpContextAccessor.HttpContext.Request.Headers;
            const string OpenId = "OpenId";
            if (header.ContainsKey(OpenId))
            {
                Console.WriteLine("OpenId = " + header[OpenId]);
                var isExist = await RedisHelper.ExistsAsync($"{OpenId}:{header[OpenId]}");
                if (isExist)
                {
                    return AuthorizationResult.Success();
                }
                return AuthorizationResult.Failed();
            }
            else
            {
                var res = await base.AuthorizeAsync(user, resource, requirements);
                return res;
            }
        }

        public new Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
        {
            Console.WriteLine("EAuthorizationService -- policyName");
            var res = base.AuthorizeAsync(user, resource, policyName);
            return res;
        }
    }

 

标签:OpenId,httpContextAccessor,res,header,var,EAuthorizationService,public
来源: https://www.cnblogs.com/jonney-wang/p/16088577.html