You receive the following message from time to time on your ASP.NET Website:
Server Error in '/' Application.
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
System.Web.Mvc.HttpAntiForgeryException: A required anti-forgery token was not supplied or was invalid.
This error is usually caused by the Application Pool process or the server recycling. By default, ASP.NET encrypts the viewstate using an Autogenerated Key when the process spins up. The problem comes when a client (browser) sends the request with a viewstate encrypted with the key generated by another worker process. Since the key is different, ASP.NET will not be able to decrypt the viewstate and it will throw the above error.
There are several ways to get around this problem:
1) Host your site on a server that never restarts or recycles. Obviously, this is not possible!
2) Configure ASP.NET to not use Auto-Generated Key but rather a predefined key. This is the preferred method.
To do this, follow these steps:
a) Either build your own Key Generator or use this tool (ASP.NET Machine Key Generator (blackbeltcoder.com)). We highly recommend you use the online tool. So assuming you will use the online tool:
b) In the online tool, simply click the "Generate Keys" button.
c) Copy the content in the textbox into your site's web.config file. The machineKey node should be within <system.web>