예제 #1
0
        public static bool UserHasClaimPermission(this HttpContext context, PermissionClaims permission)
        {
            if (context.User.Identity.IsAuthenticated)
            {
                return(context.User.HasClaim(CustomClaimTypes.Permission, permission.ToString()));
            }

            return(GetAnonymousRoles(context, permission).Result);
        }
예제 #2
0
        private static async Task <bool> GetAnonymousRoles(HttpContext httpContext, PermissionClaims permission)
        {
            //TODO validar performance
            string roleName    = permission.ToString();
            var    roleManager = httpContext.RequestServices.GetService <RoleManager <IdentityRole> >();
            var    role        = await roleManager.FindByNameAsync("Anonymous");

            var claims = await roleManager.GetClaimsAsync(role);

            var hasClaim = claims.Any(x => x.Type == CustomClaimTypes.Permission && x.Value == roleName);

            return(hasClaim);
        }
예제 #3
0
        private static Task <bool> GetAnonymousRoles(HttpContext httpContext, PermissionClaims permission)
        {
            string roleName = permission.ToString();

            var claims = httpContext.Items[GetAnonymousRolesCacheKey] as IList <Claim>;

            if (claims != null)
            {
                var hasClaimFromCache = claims.Any(x => x.Type == CustomClaimTypes.Permission && x.Value == roleName);
                return(Task.FromResult(hasClaimFromCache));
            }

            return(GetAnonymousRolesFromDb(httpContext, permission));
        }
예제 #4
0
        private static async Task <bool> GetAnonymousRolesFromDb(HttpContext httpContext, PermissionClaims permission)
        {
            string roleName = permission.ToString();

            var roleManager = httpContext.RequestServices.GetService <RoleManager <IdentityRole> >();
            var role        = await roleManager.FindByNameAsync("Anonymous");

            IList <Claim> claims = await roleManager.GetClaimsAsync(role);

            httpContext.Items[GetAnonymousRolesCacheKey] = claims;

            var hasClaim = claims.Any(x => x.Type == CustomClaimTypes.Permission && x.Value == roleName);

            return(hasClaim);
        }