protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
        {
            if (context.HasSucceeded)
            {
                // This handler is not revoking any pre-existing grants.
                return;
            }

            var claims = new HashSet <Claim>();

            foreach (var claim in _anonymousClaims ??= await GetRoleClaimsAsync("Anonymous"))
            {
                claims.Add(claim);
            }

            if (context.User.Identity.IsAuthenticated)
            {
                foreach (var claim in _authenticatedClaims ??= await GetRoleClaimsAsync("Authenticated"))
                {
                    claims.Add(claim);
                }
            }

            if (_permissionGrantingService.IsGranted(requirement, claims))
            {
                context.Succeed(requirement);
                return;
            }
        }
Ejemplo n.º 2
0
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
        {
            if (context.HasSucceeded || !(context?.User?.Identity?.IsAuthenticated ?? false))
            {
                return(Task.CompletedTask);
            }
            else if (_permissionGrantingService.IsGranted(requirement, context.User.Claims))
            {
                context.Succeed(requirement);
            }

            return(Task.CompletedTask);
        }