[代码示例]如何在ASP.NET中获取随机生成的cookie加密与验证密钥
本文是从ASP.NE T 1.1升级到ASP.NET 2.0需要考虑的Cookie问题的补充,通过示例代码说明如何通过反射在ASP.NET 1.1与ASP.NET 2.0中获取随机生成的cookie加密与验证密钥。
ASP.NET 1.1示例代码:
ASP.NET 2.0示例代码:
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进制表示的字符串
//得到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
//直接创建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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~