-
Notifications
You must be signed in to change notification settings - Fork 40
Cookie header request example #83
Comments
You'll need to send the client a new tokenset when they log in. The tokenset always has some embedded data that identifies the user. When the user is anonymous we just use a random token. When the user is logged in, we generate the token set based on their claims. Our XSRF system uses two tokens. One of them goes in the
Angular's docs explain this in more details https://docs.angularjs.org/api/ng/service/$http (ctrl+F for So the The The |
/cc @blowdart in case he wants to add anything |
Thanks @rynowak, it's a bit clearer now. |
Are you generating the new token set as part of the response right when the user logs in? You might need to add a call to /cc @HaoK |
No I'm not. I'm using ui-router to change pages, so I thought it'll be recreated in the middleware section. |
EDIT: Ok problem still exists. I've been messing with AutoValidateAntiforgeryTokenAttribute, then tricked myself thinking it was fixed when I disabled it. ORIGINAL POST: |
You can also try explicitly disabling the cookie security stamp validation to confirm that it isn't the issue by removing the OnValidatePrincipal that is added by default:
|
@HaoK Sorry, I'm not quite sure what you're saying. |
Have you tried turning on logging and seeing if any interesting failures come out? Is AutomaticAuthenticate set to true for your cookies? |
I've got logging on Debug level. I haven't set AutomaticAuthenticate, but I'll try setting it. Note that I didn't try any of these cookies settings since I didn't see them in the Sample. EDIT: OK, so |
Okay sounds like this is really an antiforgery issue. |
@HaoK he needs to update the principal after login so that AF will generate the right token. In a normal MVC non-ajax flow you'd get redirected after login so the next request which is a GET generates a new token because you have the updated logged-in principal. @kichalla - you were interested in this, want to see if you can come up with a quick sample or retrofit this on the current sample? |
Thanks for everyone's help. It seems I made a grave error. |
Yeah that would do it! Seems right to me. Glad we we able to help. |
Great. It's working. Thanks again everyone! |
I looked at the AntiforgerySample but I'm still confused how this works in an Angular SPA environment (where the majority of
[ValidateAntiforgeryToken]
is declared on the backend WebApi in my project).I have followed the same as the sample, but I will also note I have added Identity, which I think also adds the Antiforgery service for itself(?).
I have done a login page and a logoff page, where the cshtmls are 'dumb' and I have HttpPost methods LogIn and LogOff
[ValidateAntiforgeryToken]
as WebApi methods that the angular controller calls.The first time it generates the token and I can successfully log on in the Login page.
However, any subsequent WebApi calls that have
[ValidateAntiforgeryToken]
(eg. my LogOff WebApi method), I keep gettingThe provided antiforgery token was meant for a different claims-based user than the current user.
Looking at Fiddler, I see 3 cookie tokens sent on request (each with different values):
.AspNetCore.Antiforgery.xxxx
.AspNetCore.Identity.Application
XSRF-TOKEN
Am I missing something in my case? Do I need to override how the validation is working?
The text was updated successfully, but these errors were encountered: