Click here to Skip to main content
15,921,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am getting bellow error some times while running the application at production server. When running the same application at test server it working fine.

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.</machinekey>

I have found this while googling. it is using the following in the web.config of
website: enableViewStateMac="false" viewStateEncryptionMode="Never" enableEventValidation="false"

Is there any other way to fix this. Is there something in IIS or the web.config I can set to stop this message randomly coming up?
Posted
Updated 31-Jul-11 23:55pm
v2

1 solution

If it is a webfarm with dynamic load balancing, you may see this error. Basically what is happening is, a server is serving the request for a page and the page is posted back to a different server. The two servers use different keys to encrypt the viewstate information. The server that receives the postback request tries to decrypt the viewstate with its key which is different from the key it as encrypted. To fix this issue, you have to use the same key across all servers in your webfarm.
You can set this either in machine.config or your web.config. But it has to be the same across all servers.

HTML
<machinekey validationkey="<encryptionkey>" decryptionkey="<decryptionkey>" validation="SHA1" decryption="Auto" />


You can use this C# function to generate the keys:
C#
public static string CreateMachineKey(int length)
		{
			// Create a byte array.
			byte[] random = new byte[length/2];
			// Create a cryptographically strong random number generator.
			System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
			
			// Fill the byte array with random bytes.
			rng.GetBytes(random);
			
			// Create a StringBuilder to hold the result once it is
			// converted to hexadecimal format.
			System.Text.StringBuilder machineKey = new System.Text.StringBuilder(length);
			// Loop through the random byte array and append each value
			// to the StringBuilder.
			for (int i = 0; i < random.Length; i++)
			{
				machineKey.Append(String.Format("{0:X2}", random[i]));
			}
			return machineKey.ToString();
		}


The encryption key length can be between 40 and 128 and the decryption key lengh can be either 16 or 48. The recommended lengths are 128 for the encryption key and 48 for the decryption key.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900