[程式碼範例]如何在ASP.NET中擷取隨機產生的cookie加密與驗證密鑰

來源:互聯網
上載者:User
本文是從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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.