Skip to content

Fix bug on missing Authorization header#4

Open
magicleon94 wants to merge 1 commit intocuongle:masterfrom
magicleon94:patch-2
Open

Fix bug on missing Authorization header#4
magicleon94 wants to merge 1 commit intocuongle:masterfrom
magicleon94:patch-2

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