If you are looking solution for one of the weird error “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.” I can feel your pain. Few days back I was on same position where you are standing now. I was stuck and clearly had no Idea why application getting this error on only some pages where whole application was working fine.
WHAT actually Happened ?
I was assigned task to write WEB API in ASP.NET Framework 4.0. While writing WEB API I had to include some required assembly like System.Net.Http.dll, System.Web.Http.dll and Microsoft.Web.Infrastructure.dll. After successfully build of WEB API in application few pages started to get error with message “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. ” You can see error message details in below pic.
What steps I took to resolve error ?
After going through some article I noticed different people had different story for same error and everyone had their own story of resolving error. Out of many ways, finally I got solution which helped me to resolve this error. Here I am going to show you how I resolve this error step by step.
Step 1 – Open IIS. If you don’t know how to open IIS. Don’t worry, you can use simple step to open IIS manager. To open IIS Manager from the Search box Click Start. In the Start Search box, type inetmgr and press ENTER.
Step 2 – Once Internet Informations Services(IIS) Manager window is open. Select http://localhost/ – ( Your local system ). Under Sites, double click on that particular Web App(Where you are getting error). Under ASP.NET panel, open Machine Key
Step 3 – Select Validation Method as per your application requirement. Lists are –
- AES – en.wikipedia.org/wiki/Advanced_Encryption_Standard
- MD5 – en.wikipedia.org/wiki/MD5
- SHA1 – en.wikipedia.org/wiki/SHA-1
- TripleDES – en.wikipedia.org/wiki/Triple_DES
- HMACSHA256 – en.wikipedia.org/wiki/HMAC
Now select Encryption Method. Lists are –
Before selecting Validation method and Encryption method, make sure what type of encryption-decryption your application already using. Selecting wrong type may won’t work for you.
and finally click on Generate Keys.
Step 4 – You can see Validation Key and Decryption Key successfully generated. Now click on Apply button under Action Panel.
As soon as you have a <machineKey> element, you can put it in the Web.config file. The <machineKey> element is only valid in the Web.config file at the root of your application and is not valid at the subfolder level.[xml] <configuration>
<machineKey validation="Enter_validation_type" validationKey="enter_validation_key" decryption="Auto" decryptionKey="enter_descryption_Key"/>
For a full list of supported algorithms, run help Generate-MachineKey from the Windows PowerShell prompt.
Remember the view state is encrypted for security reasons and each machine.config on each web server will have a different key so they must all be the same. Best way is to add a machineKey element into each of the web server’s web.config and define the same keys and algorithm. The machineKey goes under the System.web node.
Now build project and enjoy error free application. In case of any query please write in comment. Happy Coding !!