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

首頁 > 編程 > .NET > 正文

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

2024-07-10 12:55:11
字體:
來源:轉載
供稿:網友

    本文是從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進制表示的字符串

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 雷波县| 健康| 呼玛县| 许昌市| 乐业县| 利津县| 绥芬河市| 康平县| 长沙市| 舒城县| 临安市| 宾阳县| 江油市| 承德市| 太谷县| 高清| 桐梓县| 遂溪县| 甘孜| 米脂县| 清镇市| 津南区| 建始县| 民和| 金坛市| 增城市| 彝良县| 二手房| 涞源县| 福安市| 陈巴尔虎旗| 镶黄旗| 昌邑市| 邹平县| 陆川县| 当雄县| 南昌市| 平潭县| 电白县| 莱西市| 鄱阳县|