Exemple #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);
        }
Exemple #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);
        }
Exemple #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));
        }
Exemple #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);
        }