[代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
2006-03-29 01:27
956 查看
源地址:http://dudu.cnblogs.com/archive/2006/03/15/350441.html
本文是从ASP.NE T 1.1升级到ASP.NET 2.0需要考虑的Cookie问题的补充,通过示例代码说明如何通过反射在ASP.NET 1.1与ASP.NET 2.0中获取随机生成的cookie加密与验证密钥。
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进制表示的字符串
//作者Blog: http://dudu.cnblogs.com
本文是从ASP.NE T 1.1升级到ASP.NET 2.0需要考虑的Cookie问题的补充,通过示例代码说明如何通过反射在ASP.NET 1.1与ASP.NET 2.0中获取随机生成的cookie加密与验证密钥。
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进制表示的字符串
//作者Blog: http://dudu.cnblogs.com
相关文章推荐
- (收藏)[代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- [代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
- 在ASP.NET中获取随机生成的cookie加密与验证密钥
- 生成随机验证图片--ASP.NET中常用代码之二
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- Asp.net环境下如何生成随机图片
- Asp.Net中的获取Web.config中设置的参数!(前后台的代码示例)
- ASP.NET如何实现服务器端身份验证和客户端状态管理?(Session机制和Cookie)
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- ASP.Net 2.0 窗体身份验证机制-转+自己代码注释示例与更详细的说明(网上转)
- Asp.Net中的获取Web.config中设置的参数!(前后台的代码示例)
- AjaxPro.NET框架生成高效率的Tree(Asp.net 2.0)(示例代码下载) [转]
- Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- 讲解如何让 ASP.NET 实现获取xxxxxx rank值的代码数据