-
Notifications
You must be signed in to change notification settings - Fork 597
AntiForgeryValidation attribute seems to conflict with CookieAuthenticationEvents OnRedirectToLogin event handler #1009
Comments
Reposting from https://forums.asp.net/t/2101587.aspx, From imran_ku07, I think you have orderering middleware issue. ValidateAntiForgeryToken is mvc middleware thing and Cookie-Middle-ware is seperate. Make sure it Cookie-Middle-ware is registered first. Also, it look like both Authorize and ForgeryToken are auth filters. are https://github.com/aspnet/Mvc/blob/c7a46e4caf0d1f25dd5f8bcd385af50b34b0f5dd/src/Microsoft.AspNetCore.Mvc.Core/Authorization/AuthorizeFilter.cs#L21 and https://github.com/aspnet/Mvc/blob/a78f77afde003c4a3fcf5dd7b6dc13dd9c85f825/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ValidateAntiforgeryTokenAuthorizationFilter.cs#L14 So, you may Order try https://github.com/aspnet/Mvc/blob/ee2cfa1963ee7f8195130d41330f94d4622ca89a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ValidateAntiForgeryTokenAttribute.cs#L24 |
Reposting from https://forums.asp.net/t/2101587.aspx, From Alex, Hi Vincent, Just curious if you ever found a solution for that problem. I'm currently facing a very similar one, where antiforgery token validation fails before authorization fails, and "400 - Bad request" is being returned to the client.
So, cookie authentication fails, and that's fine as some of the controllers don't require an authenticated user. But then, the antiforgery filter kicks in and fails the request (the particular controller being called does require an authenticated user, and has both the authorize and antiforgery attributes set). |
This is the first I've heard of this. It sounds like there are two bugs here. We should force AF to run after In this case, does the authorize filter let the request 'pass through' in this scenario or does it short-circuit? |
I'm not very privy to the internals of these filters, but IMHO the Authorize filter should be allowed to inspect the request and potentially redirect to login if the auth cookie ticket has expired, and do so before antiforgery validation takes place. I'm guessing the only legitimate scenario where a request would need to fail with 400 is when the auth cookie was indeed valid and unexpired, but the antiforgery token was either not present or turned out wrong for the identity specified in the (valid) auth cookie. |
@alobakov that's my understanding of the desired behavior |
I'm starting to wonder how much of this has to do with using cookie auth for WebApi scenarios. This is not recommended, JwtBearer is more appropriate. |
Based on the logic that is apparently causing this error, I'd think that it could easily manifest itself in a regular web controller as well, and the user would see the "400 - Bad request" message in their browser after trying to refresh a protected page left idle for a long time. And I'm guessing any further attempts to refresh the window would keep failing until the auth cookie (expires:session) got purged. So, they'd effectively have to restart the browser in order to purge the cookie and finally get redirected to a login page. |
@ryanbrandenburg can you try to get a repro for this and we can discuss how to proceed after that? |
@imranbaloch I was able to get this working as expected by setting the order to a number greater than 0 on With that said, @rynowak and I talked this over and we think ValidateAntiForgeryToken should default to a high order so that it goes after the Authorization filter where we expect it by default. |
FYI I moved this bug to MVC here: aspnet/Mvc#5483 And we will include it in the next 1.0.x patch release. |
Reposting from https://forums.asp.net/t/2101587.aspx,
From Vincent H,
Hi,
My app is based on the SDK: 1.0.0-preview2-003121 and uses a combination of MVC, Web API and AngularJs (v1)
When making API calls, I want to return a 401 unauthorized when the cookie based session has expired. I have adjusted the "Startup.cs" to include an "OnRedirectToLogin" event handler so that API calls can be intercepted to return 401.
However when the API controller is decorated with the "[ValidateAntiForgeryToken]", the cookie authentication event is never fired and a 400 bad request is returned instead.
Can someone assist me?
Here is my "Startup.cs" > "ConfigureServices" code:
And this is the "Startup.cs" > "Configure" code for the AntiForgeryValidation support:
The text was updated successfully, but these errors were encountered: