How To Fix the: “Validation of viewstate MAC failed” Error (ASP.NET MVC)
I run my site on a Windows Shared Hosting account, and every time I updated the assemblies on my ASP.NET MVC site I’d be presented with the “Validation of viewstate MAC failed” error.
The “Validation of viewstate MAC failed” error only occurred when a page contained an HTML form element that made use of MVC’s AntiForgeryToken. The quick fix was to delete my __RequestVerificationToken cookie, but the error would rear its ugly head the minute I touched my assemblies. The long term solution was to add a machineKey element to my Web.config file - asking visitors to delete a specific cookies when visiting my site was not a viable option.
How I fixed the “Validation of viewstate MAC failed” error on Shared Hosting:
- I used the <machineKey> Generator Tool to generate a machine key
- I added the machineKey element to my Web.config file
My Web.config now looks similar to this:
<?xml version="1.0"?>
<configuration>
<system.web>
<machineKey validationKey="..." decryptionKey="..." validation="SHA1" />
Anyhow, I hope this post helps anyone else that’s encountering this error.
Oh wait, here’s the error in its entirety for The Google Machine’s crawlers:
Server Error in '/' Application.
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that 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 configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.