System.Security.Cryptography.CryptographicException

项目背景: asp.net mvc升级到5.2.0.0,web.config的System.Web設置了machineKey

错误:在cshtml的@Html.AntiForgeryToken()抛出System.Security.Cryptography.CryptographicException异常

解决:重置web.config的静态机器码

            参考:http://msdn.microsoft.com/en-us/library/ms998288.aspx

 1:使用 RNGCryptoServiceProvider 类创建一个随机秘钥

 2:选择一个适当的秘钥大小,如下:

        1) SHA1, set the validationKey to 64 bytes (128 hexadecimal characters).
        2)AES, set the decryptionKey to 32 bytes (64 hexadecimal characters).
        3)3DES, set the decryptionKey to 24 bytes (48 hexadecimal characters).

 3:创建一个console application生成key,如下代码:

using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;

class App {
  static void Main(string[] argv) {
    int len = 128;
    if (argv.Length > 0)
      len = int.Parse(argv[0]);
    byte[] buff = new byte[len/2];
    RNGCryptoServiceProvider rng = new 
                            RNGCryptoServiceProvider();
    rng.GetBytes(buff);
    StringBuilder sb = new StringBuilder(len);
    for (int i=0; i<buff.Length; i++)
      sb.Append(string.Format("{0:X2}", buff[i]));
    Console.WriteLine(sb);
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值