-
Notifications
You must be signed in to change notification settings - Fork 595
Receiving HTTP 401 when should be receiving 403 #720
Comments
@blowdart Remember the 401/403 behavior comes entirely from our implementations of our auth middleware (and using our base AuthenticationHandlers) This behavior is by design, in that there's nothing that would turn 401 into 403s given this setup... |
Ah, there you go @chadly - you need to either use the base class, or implement it yourself. |
What base class? Is there a better way to implement it other than what I did here? Also, why not make this the default behavior? Returning a |
Returning a 403 is the correct thing to do when a user is authenticated but unauthorized. Both I still don't quite get what the proposed solution should be. Where did you move our cheese to? Why can't it just work right this time? |
@chadly There's an AuthenticationMiddlware base class, and an AuthenticationHandler base class. You'll note they have rather specific events, HandleForbiddenAsync(ChallengeContext context) and HandleUnauthorizedAsync(ChallengeContext context) As for why it's not the default, well, the behaviour is dependent on protocol. Authentication is not limited to HTTP as a protocol. Cookies, for example, redirect. Federation, depending on setup, might return to the IDP. |
@danludwig The provided middleware, cookie auth, JWT do the behaviour you expect, because that's what those protocols mandate. This would also be the default for any auth middleware deriving from the AuthenticationMiddleware base class. However it's up to the middleware developer to return whatever the status code, redirect, or other behaviour is suitable for their protocol. |
Ah, ok. That makes sense. From looking at the code, it looks like Just out of curiosity, in my example above, what auth middleware would it have been using? If I don't explicitly register any specific authentication scheme, what is it using to return to me the e.g. in |
I believe that's MVC at that point. For test users what I usually end up doing is using cookie auth and dropping the cookie - https://github.com/blowdart/AspNetAuthorizationWorkshop/blob/master/src/Step%202%20-%20Authorize%20All%20The%20Things/Controllers/AccountController.cs |
Wait, something in MVC is still setting a 401? I thought we got rid of all of that because we wanted the auth middleware to do it? |
It appears so. In my example above, the only thing I had in my |
Is this it? https://github.com/aspnet/Mvc/blob/fd3ee499872465ccc2c5534b58f8ad113a8c0190/src/Microsoft.AspNetCore.Mvc.Core/Filters/AuthorizationFilterAttribute.cs#L50 |
That certainly is what's causing the 401 |
It's sweet you think I'd know :) @HaoK do we own that or MVC? i.e. can you change it? |
Yeah this code used to return ChallengeResult when last I touched it, we should open a bug for MVC to change this back (its very counter intuitive so I can see why they switched it when they were fiddling with things) |
@Tratcher do you want open the bug? |
Logging a bug per @blowdart 's suggestion on the last comment here (also discussed here with @danludwig)
When using the
Authorize
attribute, MVC 6 is returning HTTP401
when I believe it should be returning a403
. Here are repro steps:When hitting the root endpoint, it will correctly return:
If you comment out the line
context.User = principal
in the middleware, hitting the controller will correctly return a401
(since no user is authenticated). If, however, you comment out the linenew Claim("Permission", "eat donuts")
in the middleware, it will still return a401
when it should be returning a403
(since the user is authenticated, he just doesn't have permission).Using Microsoft.AspNet.Mvc v6.0.0-rc1-final
The text was updated successfully, but these errors were encountered: