首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> ASP.NET技巧 -> 如何在ASP.NET中获取随机生成的cookie加密与验证密钥
如何在ASP.NET中获取随机生成的cookie加密与验证密钥


文章类别:ASP.NET技巧 来源: 作者: 发表日期:2006-3-18 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

本文是从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

上一篇:用Photoshop色阶轻松制作印章效果 下一篇:如何在SQLSERVER中快速有条件删除海量数据
本栏目热门文章
·如何实现无刷新的DropdownList联动效果 2005-10-4
·使用HttpWebRequest向网站模拟上传数据 2005-10-4
·当DataSet中包含主/子表时,Update更新步骤 2005-10-6
·ASP.NET2.0实现无刷新客户端回调 2005-11-13
·ASP.NET中文件上传下载方法集合 2006-5-28
·分享个极好的无刷新二级联动下拉列表,同样适用与firefox 2005-10-19
·在Web DataGrid中当鼠标移到某行与离开时行的颜色发生改变( 2005-10-4
·ASP.NET中实现Flash与.NET的紧密集成 2005-11-21
·关于Asp.net页面Page_Load被执行两次的问题 2005-10-4
·ASP.NET极限:页面导航 (翻译) 2005-10-8
新近更新文章
·在VS2003中直接用DREAMWEAVER8打开ASPX文件 2006-6-22
·asp.net2.0中异步调用WebService(异步页) 2006-6-22
·ASP.NET2.0数据库入门之常见错误 2006-6-22
·ASP.NET 2.0中XSLT的使用 2006-6-22
·.Net2.0 使用ConfigurationManager读写配置 2006-6-22
·.net 做的IP 访问限制 2006-6-22
·ASP.Net2.0 GridView 多列排序,显示排序图标,分页 2006-6-22
·数据岛出到Excel最为简易的方法 2006-6-20
·权限树中Checkbox的操作[Asp.Net2.0] 2006-6-20
·使用Data Access Application Block 得到 2006-6-20
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: