Monday, March 16, 2009

Validation of viewstate MAC failed

Recently I ran into a issue on my web site. If I opened a page and didn't do anything for a while, say 10 minutes, I would get the following error if I click a link on the page.

*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.

I sent an email to the support of my web hosting because I am not sure if my site is hosted by a Web Farm. This is their reply:

"This can happen when your application looses the session. It can loses the session when the application pool is recycled."

This didn't help me much. So I googled. Some people suggested to set EnableViewStateMac="false" in web.config. That didn't work for me.

I am thinking if my web site is running on a web farm and their machine keys do not match, there is nothing I can do about it. But I may be able to override the machine key in my web.config file. Finally I found this "Generate MachineKey Online Tool" by Scott Forsyth. I think that is exactly what I need. It says:

"Run the following tool to create a valid random machine key to place in your web.config or machine.config file.  This is beneficial in a webfarm where all of the server nodes need to have the same machine key, and it is also beneficial on a single box to keep the machine key consistent between IIS recycles and server reboots. "

I run it and copied the generated Machine Key to my web.config file. The problem is gone.

No comments:

Post a Comment