Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug on missing Authorization header #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

magicleon94
Copy link

I noticed that when the Authorization header is missing (line 21) no error is returned, hence the filter passes and the decorated API (with [JwtAuthentication]) is executed normally.
Adding context.ErrorResult assignment should fix the problem.
Since I'm not an expert in this field I apologise if my proposition is wrong, and I'd like to learn about what I've done wrong.

I noticed that when the Authorization header is missing (line 21) no error is returned, hence the filter passes and the decorated API (with [JwtAuthentication]) is executed normally.
Adding context.ErrorResult assignment should fix the problem.
Since I'm not an expert in this field I apologise if my proposition is wrong, and I'd like to learn about what I've done wrong.
@contactsamie
Copy link

this is important even for demo

@UnreachableCode
Copy link

This is a big deal. I'm surprised nobody has picked this up in the highly rated Stackoverflow question which points to it: https://stackoverflow.com/questions/40281050/jwt-authentication-for-asp-net-web-api

@tno2007
Copy link

tno2007 commented Mar 21, 2021

I have tried to add this line that you recommended...

if (authorization == null || authorization.Scheme != "Bearer")
{
    context.ErrorResult = new JwtAuthenticationFailureResult("Missing Authorization Header", request);
    return;
}

but it makes [AllowAnonymous] not work anymore.

In my authentication controller where I require [AllowAnonymous] (because I am just logging in), I get:
401 Missing Authorization Header

The initial code is working as it should.

if (authorization == null || authorization.Scheme != "Bearer")
    return;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants