Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Failed authorization for an authenticated user redirects to login page instead of access denied page #246

Closed
leastprivilege opened this issue May 6, 2015 · 15 comments
Assignees
Labels

Comments

@leastprivilege
Copy link
Contributor

A failed role check with the Authorize attribute redirects an authenticated user back to the login page. I haven't checked the internals, but that seems to indicate you are emitting a 401.

My assumption was that in that situation a 403 should be emitted and the cookie middleware will use the AccessDeniedPath instead.

@brockallen
Copy link

Yes, that API is very easy to read :)

@leastprivilege
Copy link
Contributor Author

OK - but I am returning a 403 for an authenticated user - and this code path does not seem to get hit..

@Tratcher
Copy link
Member

Tratcher commented May 6, 2015

You'd think that 403 would be the appropriate thing to return, but what they eventually settled on is returning a 401 and having the auth middleware that produced the user identity (cookies) change it to a 403 OR redirect to AccessDeniedPath.

@HaoK
Copy link
Member

HaoK commented May 6, 2015

Yep, need to convince @lodejard if you want to change this behavior...

@kevinchalet
Copy link
Contributor

Related ticket: #134

@leastprivilege
Copy link
Contributor Author

OK - fair enough.

But still it does not work.

I authenticate using cookie MW and hit a Authorize attribute with a role check - role check fails and I get redirected to login page. I have an access denied page set on the cookie MW.

Am I doing something wrong?

@Tratcher
Copy link
Member

@HaoK ?

@HaoK HaoK self-assigned this May 11, 2015
@bojanrajkovic
Copy link

So, according to @lodejard, they should already be acting like this: https://twitter.com/loudej/status/601074953836834816

In my tests with 1.0.0-beta5-12413 versions of AuthN/AuthZ packages, this isn't working -- an authenticated, but not authorized, user is still getting a 302 redirect to the login page.

@HaoK
Copy link
Member

HaoK commented May 21, 2015

Looks like the issue is 401->403 not working for automatic authentication, which is always the case for the Authorize(Roles = "Foo"). Things seem to work via an explicit policy where ActiveAuthenticationScheme includes the Cookie... Still investigating

@HaoK
Copy link
Member

HaoK commented May 21, 2015

Yup, confirmed this workaround should work @leastprivilege @bojanrajkovic can you try this and see fixes the issue?

                options.AddPolicy("Cookies", policy => {
                    policy.AddAuthenticationSchemes("Cookies");
                    policy.RequireAuthenticatedUser();
                });

And then an addition [Authorize("Cookies")] in your controllers, all this does is explicitly force the cookie middleware to get authenticate called before authorization happens.

@bojanrajkovic
Copy link

@HaoK Confirmed, that works! I added that configuration snippet and added [Authorize("Cookie")] in addition to my existing role-based authorize attribute, and now I get a 403 instead of endless redirects.

@HaoK
Copy link
Member

HaoK commented May 21, 2015

Awesome thanks, just wanted to make sure this is the same root issue, I'll work on the fix shortly...

@HaoK
Copy link
Member

HaoK commented May 22, 2015

e54d088

@HaoK HaoK closed this as completed May 22, 2015
@bojanrajkovic
Copy link

This should be in builds now, right? We don't need that workaround from earlier?

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

No branches or pull requests

6 participants