ASP.NET MVC App的本地IIS部署期间“在加密操作期间发生错误”

时间:2019-09-04 16:43:27

标签: asp.net-mvc iis

我知道还有其他与此问题相关的线程,但是我尝试了所有在这些线程中可以找到的解决方案,但没有用。我有一个ASP.NET网站,该网站使用Visual Studio提供的针对Active Directory进行身份验证的默认模板构建。然后,我尝试将其部署到计算机上的本地IIS。但是,在部署时出现此错误。我可以在IIS上在本地正常运行。我在不同端口的同一本地IIS上部署了针对同一AD进行身份验证的常规ASP.NET程序,它运行良好。但是,即使两者之间的所有配置都相同,我的应用仍然会出错。两者都通过开发证书使用HTTPS。任何意见,将不胜感激。谢谢。

Error

1 个答案:

答案 0 :(得分:0)

您需要在web.confile文件中添加机器密钥条目,如下所示:

    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <machineKey decryptionKey="Enter decryption Key here" 
                    validation="SHA1" 
                    validationKey="Enter validation Key here" />
      </system.web>  
   </configuration>

如果仍然出现错误,请尝试在iis应用程序池高级设置中设置LoadUserProfile = true。

下面是生成机器密钥的脚本:

 # 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)
  }
}

保存此脚本。加载文件。

PS C:\windows\system32> C:\Untitled1.ps1

运行以下命令:

PS C:\windows\system32> Generate-MachineKey -validationAlgorithm SHA1

您可以参考以下文章以获取更多详细信息: Resolving view state message authentication code (MAC) errors