国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何在ASP.NET中獲取隨機生成的cookie加密與驗證密鑰

2019-11-18 17:21:05
字體:
來源:轉載
供稿:網友

本文是從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.VEVb.com


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上饶市| 固阳县| 万宁市| 海城市| 普兰县| 兴安盟| 平顶山市| 个旧市| 攀枝花市| 九龙城区| 河间市| 龙岩市| 亳州市| 边坝县| 平乐县| 九龙城区| 兰州市| 如东县| 宁德市| 仪陇县| 德清县| 富蕴县| 绥滨县| 定兴县| 图木舒克市| 洱源县| 隆尧县| 洛隆县| 怀远县| 临朐县| 陕西省| 长汀县| 桃源县| 沙河市| 崇仁县| 囊谦县| 徐水县| 抚顺县| 巴楚县| 南安市| 司法|