title: How to fix the issue of Invalid viewstate
body: <p>I have developed one web application in ASP.NET(Framework 4) with C# and deployed on one external server. As I checked server's log, the <strong>Invalid ViewState</strong> exception logged periodically. To resolve this issue, I have tried many solutions but non of that worked for me. I don't want to set enableviewstatemac property to false in web.config for security reason. (May be setting this to false will resolve the issue.)</p>
  
  <p>Getting this error even for the pages that contains very small amounts of controls. </p>
  
  <p><strong>Error Description/Stacktrace:</strong></p>
  
  <pre><code>Application_Error | Invalid viewstate. 
  Client IP: [IP]
  Port: 26824
  Referer: [URL with parameter]
  Path: /page.aspx
  User-Agent: Mozilla/5.0 (Linux; Android 8.1.0; SAMSUNG SM-G610F Build/M1AJQ) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/7.4 Chrome/59.0.3071.125 Mobile Safari/537.36
  ViewState: /wEPDwUKMTU0MjgwMzYxMg8WAh4HTWVzc2FnZQULbWFsaS1icmlkZXMWAmYPZBYCAgMPZBYCAgEPZBYEAgEPDxYCHgRUZXh0BQZCcmlkZXNkZAIDDxQrAAIPFgQeC18hRGF0YUJvdW5kZx4LXyFJdGVtQ291bnQClQFkZBYCZg9kFioCAQ9kFgICAQ9kFgICAQ9kFg5mD2QWBGYPZBYEZg8VAQ9QaG90b3MvTUc4MC5qcGdkAgEPZBYCAgEPZBYCZg8VAQRNRzgwZAICD2QWAmYPFQEKMTk5Ni0xMC0wMmQCAQ9kFgICAQ9kFgJmDxUBBzVmdCAzaW5kAgIPZBYCAgEPZBYCZg8VAQdEaXBsb21hZAIDD2QWAgIBD2QWAmYPFQEiRGlwbG9tYShDaXZpbCksICBBdXRvIENhZCwgM0RTIE1heGQCBA9kFgICAQ9kFgJmDxUBDE5vdCBFbXBsb3llZGQCBQ9kFgICAQ9kFgJmDxUBDE5vdCBFbXBsb3llZGQCBg9kFgJmD2QWAgIBDw8WAh4LUG9zdEJhY2tVcmwFJX4vcHJvZmlsZS1kZXRhaWxzLmFzcHg/cHJvZmlsZWlkPU1HODBkZAICD2QWAgIBD2QWAgIBD2QWDmYPZBYEZg9kFgRmDxUBD1Bob3Rvcy9NRzc5LmpwZ2QCAQ9k...at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString, Purpose purpose)
  </code></pre>
  
  <p><strong>Try 1</strong>: Used Machine key(key for .Net 4.0) generated by one online tool in config file.</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;machineKey
  validationKey='2EEA416CEFC6D6BE856ED57B97FB9CA7DFACE17C073125949A1D682C80A44BB2A
  D887DDDC13DBFB0954F1000FEE5757E99693F222F8E28CAA2E6DAB8C4F99E0C'
  decryptionKey='877478B2F33A74226ABEF55FDCC1A76E43F1BBEA6241A592'
  validation='SHA1' /&gt;
  &lt;pages enableViewStateMac='true'/&gt;
  &lt;/system.web&gt;
  </code></pre>
  
  <p><strong>Try 2</strong>: Compressing the ViewState</p>
  
  <p>As code is long, I have added link of the code here that I have tried.<a href="https://www.codeproject.com/Articles/14733/ViewState-Compression" rel="nofollow noreferrer">Compressing the ViewState</a></p>
  
  <p><strong>Try 3</strong>: MaxPageStateFieldLength property of page in web.config.</p>
  
  <p>In this way, in web.config file, it showing warning as MaxPageStateFieldLength attribute is not allowed.</p>
  
  <p>Code:</p>
  
  <pre><code>  &lt;system.web&gt;
      &lt;pages MaxPageStateFieldLength="20"/&gt;
    &lt;/system.web&gt;
  </code></pre>
  
  <p><strong>Try 4</strong>: Place hidden fields for Encrypted Viewstate and Event validation on the top of the form</p>
  
  <p>In this way, I have override the Render event of Page and write the code taken from this link: <a href="https://blogs.msdn.microsoft.com/tom/2008/03/14/validation-of-viewstate-mac-failed-error/" rel="nofollow noreferrer">Link</a> </p>
  
  <p>None of the above solutions worked for me. Please correct me if I have did any thing wrong in above ways. Please ask for any other inputs if more clarification is needed.</p>
  
  <p>Currently I have provided one workaround. I have catched this specific exception in application's error event and redirect to same page that causing this issue. But I am not satisfied with this workaround as issue is still exist.</p>
  
  <p><strong>Workaround</strong>:</p>
  
  <pre><code>   void Application_Error(object sender, EventArgs e)
      {
          Exception exception = Server.GetLastError();
  
          if (exception != null)
          {
              if (exception is System.Web.HttpException &amp;&amp; exception.InnerException is System.Web.UI.ViewStateException)
              {
                  Response.Redirect(Request.Url.AbsoluteUri);
                  WriteLog("Application_Error", "Invalid viewstate | Redirect to: " + Request.Url.AbsoluteUri);
              }
              else
              {
                  WriteLog("Application_Error", exception.InnerException.Message + "|" + exception.StackTrace + "|" + Request.RawUrl);
              }
          }
          // Clear the error from the server
          Server.ClearError();
      }
  </code></pre>