title: Erratic Invalid Viewstate issue in a .NET application
body: <p>I seem to be getting a "invalid viewstate" every now and then in the event viewer for my <a href="http://en.wikipedia.org/wiki/ASP.NET" rel="noreferrer">ASP.NET</a> application.</p>
  
  <p>Most of them (95%) seem to be referencing <code>ScriptResource.axd</code> (the application 
  uses the <a href="http://en.wikipedia.org/wiki/ASP.NET_AJAX" rel="noreferrer">ASP.NET AJAX</a> library).  There is no way I can remove the <a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29" rel="noreferrer">Ajax</a> library either as Ajax is used everywhere..</p>
  
  <p>How can I reduce these errors? I'm getting ~ 100-200 errors a day and I have no idea how to fix them! They come from different browsers, different IPs and geographical locations.</p>
  
  <p>It's difficult for me to reproduce the problem because it barely even happened to me, it has only happened to me 3-4 times out of the blue.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Process information: 
      Process ID: 4004 
      Process name: w3wp.exe 
      Account name: NT AUTHORITY\NETWORK SERVICE 
  
  Exception information: 
      Exception type: HttpException 
      Exception message: Invalid viewstate. 
  
  Request information: 
      Request URL: http://domainnamehere/ScriptResource.axd?d=W1R6x9VzZ2C9SKnIkOmX9VRLhSjJ3nOF1GSQvPwKS3html 
      Request path: /ScriptResource.axd 
      User host address: 124.177.170.75 
      User:  
      Is authenticated: False 
      Authentication Type:  
      Thread account name: NT AUTHORITY\NETWORK SERVICE 
  
  Thread information: 
      Thread ID: 1 
      Thread account name: NT AUTHORITY\NETWORK SERVICE 
      Is impersonating: False 
      Stack trace:    at System.Web.UI.Page.DecryptStringWithIV(String s, IVType ivType)
     at System.Web.UI.Page.DecryptString(String s)
     at System.Web.Handlers.ScriptResourceHandler.DecryptParameter(NameValueCollection queryString)
     at System.Web.Handlers.ScriptResourceHandler.ProcessRequestInternal(HttpResponse response, NameValueCollection queryString, VirtualFileReader fileReader)
     at System.Web.Handlers.ScriptResourceHandler.ProcessRequest(HttpContext context)
     at System.Web.Handlers.ScriptResourceHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext context)
     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  
  
  Custom event details: 
  
  For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
  </code></pre>
  
  <p>I also get this error every now and then in my .NET code which happens at the same time which might be related:</p>
  
  <pre><code>Exception raised in GLOBAL.ASAX.Application_Error(): 'Padding is invalid and cannot be removed.' at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]&amp; outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
     at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
     at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
     at System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, IVType ivType, Boolean useValidationSymAlgo)
     at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
  </code></pre>