How do I solve an AntiForgeryToken exception that occurs after an iisreset in my ASP.Net MVC app?

后端 未结 6 736
不思量自难忘°
不思量自难忘° 2020-12-04 11:12

I’m having problems with the AntiForgeryToken in ASP.Net MVC. If I do an iisreset on my web server and a user continues with their session they get bounced to a login page.

6条回答
  •  感动是毒
    2020-12-04 11:37

    If your MachineKey is set to AutoGenerate, then your verification tokens, etc won't survive an application restart - ASP.NET will generate a new key when it starts up, and then won't be able to decrypt the tokens correctly.

    If you are seeing this a lot, I'd suggest:

    1. Configuring a static MachineKey (you should be able to do this at the application level), see "How to: Configure a MachineKey" for more information
    2. Try not to perform IIS Resets when the site is being used1

    1 The best way to do this is by having a loadbalanced application, which will require you to set a static MachineKey. Another option is to take the site down by placing a file named app_offline.htm in the root of the site, which will take the site offline and display your message - at least the users will expect things to go wrong.

提交回复
热议问题