Пример #1
0
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ClaimsRoleRequirement requirement)
        {
            User user = await _userManager.GetUserAsync(context.User);

            if (user == null || context.User.Identity.IsAuthenticated == false)
            {
                context.Fail();
                return;
            }
            string userId = user.Id.ToString();
            EClaim claim  = requirement.eClaimValue;

            var roleClaims = await _dbContext.RoleClaims
                             //.Where(rc => rc.ClaimType == claim.ToString() && rc.ClaimValue == claim.Description())
                             .Where(rc => rc.ClaimType == claim.ToString())
                             .Select(r => r.RoleId)
                             .Distinct()
                             .ToListAsync();

            var userRole = await _dbContext.UserRoles
                           .Where(ur => ur.UserId == user.Id)
                           .Select(r => r.RoleId)
                           .Distinct()
                           .ToListAsync();

            bool hasSameElements = roleClaims.Intersect(userRole).Any();

            if (hasSameElements)
            {
                context.Succeed(requirement);
            }
            else
            {
                context.Fail();
                return;
            }
        }
Пример #2
0
 public static string GetClaimName(this EClaim eClaim) // convenience method
 {
     return(eClaim.ToString());
 }