Site icon port135.com

Solved: “Validation of viewstate MAC failed”

You may come across this error message when you get around in pages of your ASP.NET website:

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.

Background

ASP.NET uses view-state variable to rebuild pages after post-backs. The text of your buttons and the value of the form fields are the examples that this variable stores.

In order to prevent tempering attacks that try to play around with view-state data to force your webpage behave unexpectedly, web server validates the view-state data between page redirections. If the data doesn’t match, you receive the error message above.

Solution

This Microsoft article summarizes the possible causes and solutions. Based on my experience, creating a new machine key and adding it to the web.config file is the most preferred solution (Example).

Steps in high-level:

  1. Open IIS Manager. Go to “Machine Key” module
  2. Unselect all options and click “Generate Keys”
  3. Copy both validation and decryption keys into notepad
  4. Click “Apply”
  5. Add the line below to your web.config file
  6. Deploy your application and test
<configuration>
     <system.web>
        <machineKey decryptionKey="xx"
 validationKey="xx" />
    </system.web>
</configuration> 

The issue of unmatched view-state data could be related to server configuration or session cookie as well. Here are the most common root causes:

If nothing works, you may try overriding SavePageStateToPersistenceMedium method to implement a way for keeping the same view state value across the requests.

Exit mobile version