MachineKey生成

 https://support.microsoft.com/zh-cn/help/2915218/resolving-view-state-message-authentication-code-mac-errors#appendixa

安全警告:
只需单击一下按钮,您就可以通过许多网站生成 <machineKey> 元素。切勿使用通过这些网站获得的 <machineKey> 元素。这些密钥是否安全创建或者是否记录到秘密数据库,都无从而知。必须使用您自己创建的 <machineKey> 配置元素。

 

打开PowerShell然后粘贴以下代码回车

 1 # 生成一个可复制并粘贴到 Web.config 文件中的 <machineKey> 元素。
 2 function Generate-MachineKey {
 3 [CmdletBinding()]
 4 param (
 5 [ValidateSet("AES", "DES", "3DES")]
 6 [string]$decryptionAlgorithm = 'AES',
 7 [ValidateSet("MD5", "SHA1", "HMACSHA256", "HMACSHA384", "HMACSHA512")]
 8 [string]$validationAlgorithm = 'HMACSHA256'
 9   )
10 process {
11 function BinaryToHex {
12 [CmdLetBinding()]
13 param($bytes)
14 process {
15 $builder = new-object System.Text.StringBuilder
16 foreach ($b in $bytes) {
17 $builder = $builder.AppendFormat([System.Globalization.CultureInfo]::InvariantCulture, "{0:X2}", $b)
18             }
19 $builder
20         }
21     }
22 switch ($decryptionAlgorithm) {
23 "AES" { $decryptionObject = new-object System.Security.Cryptography.AesCryptoServiceProvider }
24 "DES" { $decryptionObject = new-object System.Security.Cryptography.DESCryptoServiceProvider }
25 "3DES" { $decryptionObject = new-object System.Security.Cryptography.TripleDESCryptoServiceProvider }
26     }
27 $decryptionObject.GenerateKey()
28 $decryptionKey = BinaryToHex($decryptionObject.Key)
29 $decryptionObject.Dispose()
30 switch ($validationAlgorithm) {
31 "MD5" { $validationObject = new-object System.Security.Cryptography.HMACMD5 }
32 "SHA1" { $validationObject = new-object System.Security.Cryptography.HMACSHA1 }
33 "HMACSHA256" { $validationObject = new-object System.Security.Cryptography.HMACSHA256 }
34 "HMACSHA385" { $validationObject = new-object System.Security.Cryptography.HMACSHA384 }
35 "HMACSHA512" { $validationObject = new-object System.Security.Cryptography.HMACSHA512 }
36     }
37 $validationKey = BinaryToHex($validationObject.Key)
38 $validationObject.Dispose()
39 [string]::Format([System.Globalization.CultureInfo]::InvariantCulture,
40 "<machineKey decryption=`"{0}`" decryptionKey=`"{1}`" validation=`"{2}`" validationKey=`"{3}`" />",
41 $decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
42 $validationAlgorithm.ToUpperInvariant(), $validationKey)
43   }
44 }

 

对于 ASP.NET 4.0 应用程序,您只需调用 Generate-MachineKey(不带参数),就能生成 <machineKey> 元素,如下所示:

PS> Generate-MachineKey
<machineKey decryption="AES" decryptionKey="..." validation="HMACSHA256" validationKey="..."/>

 

ASP.NET 2.0 和 3.5 应用程序不支持 HMACSHA256。您可以改为指定 SHA1 来生成一个兼容的 <machineKey> 元素,如下所示:

 

PS> Generate-MachineKey -validation sha1
<machineKey decryption="AES" decryptionKey="..." validation="SHA1" validationKey="..."/>

  

转载于:https://www.cnblogs.com/Excelsior/p/7390290.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值