ASP.NET MVC - ValidateAntiForgeryToken expiring

ぐ巨炮叔叔 提交于 2019-12-03 06:22:31

I'm dealing with this same problem and while I understand the issue, I'm not sure yet of the best resolution.

The Anti-ForgeryToken process places an input value in the form with a second value stored in a cookie RequestVerificationToken. Both of these are submitted to the server and if they don't match the error is thrown.

The RequestVerficationToken cookie has an expiration value set to be Session. So when the user leaves the browser open on the page for a long time and then submits, the cookie's time stamp is compared to the session timeout value on the server — a default of 20 minutes or so — and having been exceeded, it is removed and thus token validation fails.

Possible solutions, all of which have potential issues;

  1. Put a javascript timer on the page and refresh at some value less than your session timeout.
  2. Catch the System.Web.Mvc.HttpAntiForgeryException on the server — and redirect to the same page.
  3. Increase your session timeout
  4. Change the expiration on the anti-forgery token
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!