Asp.net Validation of viewstate MAC failed

前端 未结 16 1593
猫巷女王i
猫巷女王i 2020-12-02 20:12

I am receiving the following error at certain times on asp.net website.

 Sys.WebForms.PageRequestManagerServerErrorException: 
 Validation of viewstate MAC f         


        
相关标签:
16条回答
  • 2020-12-02 20:32

    If you're using a web farm and running the same application on multiple computers, you need to define the machine key explicitly in the machine.config file:

    <machineKey validationKey="JFDSGOIEURTJKTREKOIRUWTKLRJTKUROIUFLKSIOSUGOIFDS..." decryptionKey="KAJDFOIAUOILKER534095U43098435H43OI5098479854" validation="SHA1" />
    

    Put it under the <system.web> tag.

    The AutoGenerate for the machine code can not be used. To generate your own machineKey see this powershell script: https://support.microsoft.com/en-us/kb/2915218#bookmark-appendixa

    0 讨论(0)
  • 2020-12-02 20:32

    I am not sure how this happened but I started to get this error in my internal submit form pages. So when ever I submit something I'm getting this error. But the problem is this website is almost working 5-6 years. I don't remember I made an important change.

    None of the solutions worked for me.

    I have setup a machine key with the Microsoft script and copied into my web.config

    I have executed asp.net regiis script.

    aspnet_regiis -ga "IIS APPPOOL\My App Pool"
    

    Also tried to add this code into the page:

    enableViewStateMac="false"
    

    still no luck.

    Any other idea to solve this issue?

    UPDATE:

    Finally I solved the issue. I had integrated my angular 4 component into my asp.net website. So I had added base href into my master page. So I removed that code and it is working fine now.

    <base href="/" />
    
    0 讨论(0)
  • 2020-12-02 20:34

    I had this same issue and it was due to a Gridview (generated from a vb code) on the page which had sorting enabled. Disabling Sort fixed my issue. I do not have this problem with the gridviews created using a SQLdatasource.

    0 讨论(0)
  • 2020-12-02 20:40
    <system.web>
    <pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode ="Never" />
    </system.web>
    
    0 讨论(0)
  • 2020-12-02 20:44

    I have faced the similar issue on my website hosted on IIS. This issue generally because of IIS Application pool settings. As application pool recycle after some time that caused the issue for me.

    Following steps help me to fix the issue:

    1. Open App pool of you website on IIS.
    2. Go to Advance settings on right hand pane.
    3. Scroll down to Process Model
    4. Change Idle Time-out minutes to 20 or number of minutes you don't want to recycle your App pool.

    Then try again . It will solve your issue.

    0 讨论(0)
  • 2020-12-02 20:45

    On multi-server environment, this error likely occurs when session expires and another instance of an application is resorted with same session id and machine key but on a different server. At first, each server produce its own machine key which later is associated with a single instance of an application. When session expires and current server is busy, the application is redirected like, via load balancer to a more operational server. In my case I run same app from multiple servers, the error message:

    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

    Defining the machine code under in web.config have solve the problem. But instead of using 3rd party sites for code generation which might be corrupted, please run this from your command shell: Based on microsoft solution 1a, https://support.microsoft.com/en-us/kb/2915218#AppendixA

    # Generates a <machineKey> element that can be copied + pasted into a Web.config file.
    function Generate-MachineKey {
      [CmdletBinding()]
      param (
        [ValidateSet("AES", "DES", "3DES")]
        [string]$decryptionAlgorithm = 'AES',
        [ValidateSet("MD5", "SHA1", "HMACSHA256", "HMACSHA384", "HMACSHA512")]
        [string]$validationAlgorithm = 'HMACSHA256'
      )
      process {
        function BinaryToHex {
            [CmdLetBinding()]
            param($bytes)
            process {
                $builder = new-object System.Text.StringBuilder
                foreach ($b in $bytes) {
                  $builder = $builder.AppendFormat([System.Globalization.CultureInfo]::InvariantCulture, "{0:X2}", $b)
                }
                $builder
            }
        }
        switch ($decryptionAlgorithm) {
          "AES" { $decryptionObject = new-object System.Security.Cryptography.AesCryptoServiceProvider }
          "DES" { $decryptionObject = new-object System.Security.Cryptography.DESCryptoServiceProvider }
          "3DES" { $decryptionObject = new-object System.Security.Cryptography.TripleDESCryptoServiceProvider }
        }
        $decryptionObject.GenerateKey()
        $decryptionKey = BinaryToHex($decryptionObject.Key)
        $decryptionObject.Dispose()
        switch ($validationAlgorithm) {
          "MD5" { $validationObject = new-object System.Security.Cryptography.HMACMD5 }
          "SHA1" { $validationObject = new-object System.Security.Cryptography.HMACSHA1 }
          "HMACSHA256" { $validationObject = new-object System.Security.Cryptography.HMACSHA256 }
          "HMACSHA385" { $validationObject = new-object System.Security.Cryptography.HMACSHA384 }
          "HMACSHA512" { $validationObject = new-object System.Security.Cryptography.HMACSHA512 }
        }
        $validationKey = BinaryToHex($validationObject.Key)
        $validationObject.Dispose()
        [string]::Format([System.Globalization.CultureInfo]::InvariantCulture,
          "<machineKey decryption=`"{0}`" decryptionKey=`"{1}`" validation=`"{2}`" validationKey=`"{3}`" />",
          $decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
          $validationAlgorithm.ToUpperInvariant(), $validationKey)
      }
    }
    

    Then:

    For ASP.NET 4.0

    Generate-MachineKey
    

    Your key will look like: <machineKey decryption="AES" decryptionKey="..." validation="HMACSHA256" validationKey="..." />

    For ASP.NET 2.0 and 3.5

    Generate-MachineKey -validation sha1
    

    Your key will look like: <machineKey decryption="AES" decryptionKey="..." validation="SHA1" validationKey="..." />

    0 讨论(0)
提交回复
热议问题