在ASP.NET中获取随机生成的cookie加密与验证密钥
2008-06-02 13:27
633 查看
ASP.NET 1.1示例代码:
object machineKeyConfig = HttpContext.Current.GetConfig("system.web/machineKey");
//得到System.Web.Configuration.MachineKey+MachineKeyConfig的实例,MachineKeyConfig是MachineKey的嵌套类
Type machineKeyType = machineKeyConfig.GetType().Assembly.GetType("System.Web.Configuration.MachineKey");
//得到System.Web.Configuration.MachineKey类型
BindingFlags bf = BindingFlags.NonPublic | BindingFlags.Static;
//设置绑定标志
MethodInfo byteArrayToHexString = machineKeyType.GetMethod("ByteArrayToHexString", bf);
//通过反射获取MachineKey中的ByteArrayToHexString方法,该方法用于将字节数组转换为16进制表示的字符串
Byte[] validationKey = (Byte[])machineKeyType.GetField("s_validationKey",bf).GetValue(machineKeyConfig);
//获取验证密钥字节数组
SymmetricAlgorithm algorithm = (SymmetricAlgorithm)machineKeyType.GetField("s_oDes",bf).GetValue(machineKeyConfig);
Byte[] decryptionKey = algorithm.Key;
//获取加密密钥字节数组
string ValidationKey = (string)byteArrayToHexString.Invoke(null,new object[]{validationKey,validationKey.Length});
//将验证密钥字节数组转换为16进制表示的字符串
string DecryptionKey = (string)byteArrayToHexString.Invoke(null,new object[]{decryptionKey,decryptionKey.Length});
//将加密密钥字节数组转换为16进制表示的字符串
ASP.NET 2.0示例代码:
System.Web.Configuration.MachineKeySection machineKeySection = new System.Web.Configuration.MachineKeySection();
//直接创建MachineKeySection的实例,ASP.NET 2.0中用machineKeySection取代ASP.NET 1.1中的MachineKey,并且可以直接访问,没有被internal保护。
Type type = typeof(System.Web.Configuration.MachineKeySection);//或者machineKeySection.GetType();
PropertyInfo propertyInfo = type.GetProperty("ValidationKeyInternal", BindingFlags.NonPublic | BindingFlags.Instance);
Byte[] validationKeyArray = (Byte[])propertyInfo.GetValue(machineKeySection, null);
//获取随机生成的验证密钥字节数组
propertyInfo = type.GetProperty("DecryptionKeyInternal", BindingFlags.NonPublic | BindingFlags.Instance);
Byte[] decryptionKeyArray = (Byte[])propertyInfo.GetValue(machineKeySection, null);
//获取随机生成的加密密钥字节数组
MethodInfo byteArrayToHexString = type.GetMethod("ByteArrayToHexString", BindingFlags.Static | BindingFlags.NonPublic);
//通过反射获取MachineKeySection中的ByteArrayToHexString方法,该方法用于将字节数组转换为16进制表示的字符串
string validationKey = (string)byteArrayToHexString.Invoke(null, new object[] { validationKeyArray, validationKeyArray.Length });
//将验证密钥字节数组转换为16进制表示的字符串
string DecryptionKey = (string)byteArrayToHexString.Invoke(null, new object[] { decryptionKeyArray, decryptionKeyArray.Length });
//将加密密钥字节数组转换为16进制表示的字符串
object machineKeyConfig = HttpContext.Current.GetConfig("system.web/machineKey");
//得到System.Web.Configuration.MachineKey+MachineKeyConfig的实例,MachineKeyConfig是MachineKey的嵌套类
Type machineKeyType = machineKeyConfig.GetType().Assembly.GetType("System.Web.Configuration.MachineKey");
//得到System.Web.Configuration.MachineKey类型
BindingFlags bf = BindingFlags.NonPublic | BindingFlags.Static;
//设置绑定标志
MethodInfo byteArrayToHexString = machineKeyType.GetMethod("ByteArrayToHexString", bf);
//通过反射获取MachineKey中的ByteArrayToHexString方法,该方法用于将字节数组转换为16进制表示的字符串
Byte[] validationKey = (Byte[])machineKeyType.GetField("s_validationKey",bf).GetValue(machineKeyConfig);
//获取验证密钥字节数组
SymmetricAlgorithm algorithm = (SymmetricAlgorithm)machineKeyType.GetField("s_oDes",bf).GetValue(machineKeyConfig);
Byte[] decryptionKey = algorithm.Key;
//获取加密密钥字节数组
string ValidationKey = (string)byteArrayToHexString.Invoke(null,new object[]{validationKey,validationKey.Length});
//将验证密钥字节数组转换为16进制表示的字符串
string DecryptionKey = (string)byteArrayToHexString.Invoke(null,new object[]{decryptionKey,decryptionKey.Length});
//将加密密钥字节数组转换为16进制表示的字符串
ASP.NET 2.0示例代码:
System.Web.Configuration.MachineKeySection machineKeySection = new System.Web.Configuration.MachineKeySection();
//直接创建MachineKeySection的实例,ASP.NET 2.0中用machineKeySection取代ASP.NET 1.1中的MachineKey,并且可以直接访问,没有被internal保护。
Type type = typeof(System.Web.Configuration.MachineKeySection);//或者machineKeySection.GetType();
PropertyInfo propertyInfo = type.GetProperty("ValidationKeyInternal", BindingFlags.NonPublic | BindingFlags.Instance);
Byte[] validationKeyArray = (Byte[])propertyInfo.GetValue(machineKeySection, null);
//获取随机生成的验证密钥字节数组
propertyInfo = type.GetProperty("DecryptionKeyInternal", BindingFlags.NonPublic | BindingFlags.Instance);
Byte[] decryptionKeyArray = (Byte[])propertyInfo.GetValue(machineKeySection, null);
//获取随机生成的加密密钥字节数组
MethodInfo byteArrayToHexString = type.GetMethod("ByteArrayToHexString", BindingFlags.Static | BindingFlags.NonPublic);
//通过反射获取MachineKeySection中的ByteArrayToHexString方法,该方法用于将字节数组转换为16进制表示的字符串
string validationKey = (string)byteArrayToHexString.Invoke(null, new object[] { validationKeyArray, validationKeyArray.Length });
//将验证密钥字节数组转换为16进制表示的字符串
string DecryptionKey = (string)byteArrayToHexString.Invoke(null, new object[] { decryptionKeyArray, decryptionKeyArray.Length });
//将加密密钥字节数组转换为16进制表示的字符串
相关文章推荐
- 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- [代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- [代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- (收藏)[代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- Asp.Net使用加密cookie代替session验证用户登录状态 源码分享
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
- Asp.Net使用加密cookie代替session验证用户登录状态 源码分享 欢迎拍砖
- 生成随机验证图片--ASP.NET中常用代码之二
- Asp.Net的Forms验证,解决Cookie和Seesion失效时间
- ASP.Net 加密 Cookie
- (转)ASP.Net 加密 Cookie 收藏
- ASP.NET中常用功能代码总结——生成验证图片
- ASP.NET的用户验证方法,及加密处理
- Asp.net环境下如何生成随机图片
- asp.net创建位图生成验证图片类(验证码类)
- asp.net验证码生成和刷新及验证
- ASP.NET中常用功能代码总结(2)——生成验证图片
- 浅析ASP.NET生成随机密码