title: Encountering "Validation of viewstate MAC failed"
body: <p>We have an <strong>ASP.NET 4.0</strong> web working on a <strong>Windows Server 2008 R2</strong> (installed on an Amazon ec2 instance). Every now and then, our users receive an error like this:</p>
  
  <pre><code>Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that &lt;machineKey&gt; configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
  
  Source: System.Web
  
  StackTrace:    
  at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
  at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
  at System.Web.UI.ScriptManager.LoadHistoryState(String serverState)
  at System.Web.UI.ScriptManager.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  at System.Web.UI.Page.ProcessRequest()
  at System.Web.UI.Page.ProcessRequest(HttpContext context)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  
  TargetSite: Void ThrowMacValidationError(System.Exception, System.String)
  
  System.Web.HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that &lt;machineKey&gt; configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. ---&gt; System.Web.UI.ViewStateException: Invalid viewstate. 
  Client IP: Some IP
  Port: 49314
  Referer: http://www.xxxx.com/xxxxxxx
  Path: /xxxxxxx
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
  ViewState: /wEXAQUMVmVyUHJvZHVjdG9zBQxQVkQgQVNDIzQjMTCkifeeYf3AT+qOX2HL/wiZX7nF6E2sPjS/OhlzF+pfNg==
  
  at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
  at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
  at System.Web.UI.ScriptManager.LoadHistoryState(String serverState)
  at System.Web.UI.ScriptManager.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  at System.Web.UI.Page.ProcessRequest()
  at System.Web.UI.Page.ProcessRequest(HttpContext context)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>We have searched for a solution and nothing seems to work. We've added a machine key and the error appears too. This error started to appear when we moved everthing to EC2. On our own old dedicated server there was no problem.</p>
  
  <p><strong>NOTE: Setting enableEventValidation="false" is not an option.</strong></p>