Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two web servers behind a F5 load balancer. Each web server has identical applications to the other. There was no issue until the config of the load balancer changed from source address persistence to least connections.

Now in some applications I receieve this error

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.

Source Error:

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

  1. Add a "Debug=true" directive at the top of the file that generated the error. Example:


2) Add the following section to the configuration file of your application:

Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.

How do I add a machine key to the machine.config file? Do I do it at server level in IIS or at website/application level for each site? Does the validation and decryption keys have to be the same across both web servers or are they different? Should they be different for each machine.config version of .net?

I cannot find any documentation of this scenario.

share|improve this question

migrated from Nov 5 '12 at 16:51

This question came from our site for professional and enthusiast programmers.

up vote 6 down vote accepted

If you only need it for your web site, you can add it to your web site's web.config. If there are multiple sites/applications that need to use the same machineKey for encrypting/decrypting, that is when you would use a machine-scoped configuration file.

And yes, they should be the same across all servers in the farm, and you should do this for each .NET folder's machine.config (.NET 2.0 + 4.0, and x86 + x64, so you may be updating four files).

Here is some sample code you may use to generate your keys:

/// <summary>
/// </summary>
public static string CreateMachineKey(int characterLength)
     * decryptionKey:
     * DES: 64 bits (16 hexadecimal characters)
     * 3DES:192 bits (48 hexadecimal characters)
     * AES: 128 bits (32 characters), 192 bits (48 characters), or 256 bits (64 characters)
        AES requires a 256-bit key (64 hexadecimal characters).
        MD5 requires a 128-bit key (32 hexadecimal characters).
        SHA1 requires a 160-bit key (40 hexadecimal characters).
        3DES requires a 192-bit key (48 hexadecimal characters).
        HMACSHA256 requires a 256-bit key (64 hexadecimal characters).
        HMACSHA384 requires a 384-bit key (96 hexadecimal characters).
        HMACSHA512 requires a 512-bit key (128 hexadecimal characters).

    byte[] byteArray = new byte[characterLength / 2];
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    StringBuilder sb = new StringBuilder(characterLength);
    for (int i = 0; i < byteArray.Length; i++)
        sb.Append(string.Format("{0:X2}", byteArray[i]));
    return sb.ToString();

Here is an example of a machine.config:




share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.