Aspnetcore.docs: Document the fact that custom AuthorizationHandlers will still execute even if *authentication* fails

Created on 20 Mar 2019  Â·  5Comments  Â·  Source: dotnet/AspNetCore.Docs

When using requirement-based policies, it seems that all custom AuthorizationHandlers will still execute even if token validation fails. (For example expired token.)

Why does this matter?
Some AuthorizationHandlers could perform some relatively time-intensive/expensive tasks. If _authentication_ fails, there is a high chance users wouldn't want their custom AuthorizationHandlers to start evaluating requirements.

Rough draft
"Note: If token authentication fails (due to expired token, invalid cryptographic signature, etc), you may want to exit the AuthorizationHandler.HandleAsync() early. You can do this by checking context.Principal.Identity.IsAuthenticated:"

        public Task HandleAsync(AuthorizationHandlerContext context)
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                return Task.CompletedTask;
            }

            // Perform expensive authorization
        }

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 Source - Docs.ms

Most helpful comment

Sure, I'd be happy to take a look at this.

All 5 comments

@blowdart should I doc this? I don't see anywhere that indicates they don't run when auth fails.

They also run even if another has failed, because there may be side effects like logging a developer wants. So sure, go for it.

@DrEsteban or @serpent5 are you interested in creating a PR for this?

Sure, I'd be happy to take a look at this.

Thanks @serpent5 and @Rick-Anderson!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

royshouvik picture royshouvik  Â·  3Comments

danroth27 picture danroth27  Â·  3Comments

Raghumu picture Raghumu  Â·  3Comments

nenmyx picture nenmyx  Â·  3Comments

Mattacks picture Mattacks  Â·  3Comments