Asp.net Validation of viewstate MAC failed

前端 未结 16 1632
猫巷女王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: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  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,
          "",
          $decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
          $validationAlgorithm.ToUpperInvariant(), $validationKey)
      }
    }
    

    Then:

    For ASP.NET 4.0

    Generate-MachineKey
    

    Your key will look like:

    For ASP.NET 2.0 and 3.5

    Generate-MachineKey -validation sha1
    

    Your key will look like:

提交回复
热议问题