RequestVerificationToken does not match

后端 未结 3 1385
攒了一身酷
攒了一身酷 2020-12-04 12:48

I have a problem with the anti CRSF MVC mechanism. The cookie and the form input returned does not match. I\'m getting an error every single time, only in one specific page.

相关标签:
3条回答
  • 2020-12-04 13:25

    I've had and resolved several issues with ValidateAntiForgeryToken lately, so I'll share my findings with you.

    Salt: Since you mention this only happens on a single page, my best guess is that you are using different salt values in your calls to Html.AntiForgeryToken(salt) and ValidateAntiForgeryToken(salt) calls.

    AJAX: as another answer has said, using AJAX may require extra work to ensure the token is included in the POST. Here is my favorite simple, automatic solution to add the token to all AJAX POST requests.
    In your question though, you state that you have verified that the token is sending. Have you verified that you're only sending the token once? I found out that an AJAX call of mine was sending the token twice, which combined the values, and caused it to fail.

    Machine Key and Cookies: this issue is ugly, easy to spot (causes exceptions), but not very intuitive. The validation cookies and tokens are encoded and decoded using a unique "machine key". This means that if you have a server farm, or change your server, your cookie will no longer be valid. Closing your browser fixes the issue (because the cookie is a session cookie). However, some people leave their browser windows open in the background for a long time!
    The solution is to set a "machine key" in your config file. This will tell MVC to use the same key on all servers, ensuring that the cookie will be decryptable everywhere.

    Encoding Bugs: using a testing utility called jMeter, we attempted to load-test our pages, only to find out that it had a bug that caused our token to have 2 extra " around the value.
    The solution is to lower your trust in your tools! Test in a browser, and if that works, create a test that extracts the token and cookie values, and set a breakpoint to verify the results.

    If none of these things work for you, then I'd recommend taking a look at the MVC source code for ValidateAntiForgeryTokenAttribute, specifically the OnAuthorization method. It will help you see the different steps where validation could fail. You might even inspect your error's Exception.StackTrace to determine which part is failing.

    As a side note, I really dislike the implementation of ValidateAntiForgeryToken in MVC, because:

    • There are about 5 verification steps that can fail, but there is only one generic error message.
    • The class is sealed, so it cannot be extended with additional functionality.
    • The encryption method is weird - it initializes a Page and creates an artificial ViewState to encrypt the tokens and cookies. Seems overkill.

    So, I grabbed the source code, and created my own specialized subclass, which also turned out to be very helpful in debugging its issues, because I could set breakpoints on the validation methods, and it was really easy to determine which validation step was failing.

    0 讨论(0)
  • 2020-12-04 13:29

    From my recent findings ...

    If you set content type as "application/x-www-form-urlencoded" in the ajax request then you must put the AFRT in the data

    If you set the content type to "application/json" then the token goes in the ajax "headers" property as described by haack.

    On the server if you are checking for the form type token then using the vanilla AntiForgeryRequestTokenAttribute is ok but if you want to validate tokens sent in the header then you need to call the AntiForgeryToken.OnAuthorize ... or whatever, passing the token from the cookie (http context).

    It aint easy but if it was everybody would be doing it :)

    0 讨论(0)
  • 2020-12-04 13:30

    If this is being sent as an Ajax request, then the current setup of the framework isn't build to do this naturally.

    Luckly Phil Haak wrote a nice blog post on dealing with CSRF and Ajax -> Preventing CSRF With Ajax which goes into some good detail about how to use the existing framework and modify it to work for Ajax/Json.

    0 讨论(0)
提交回复
热议问题