MachineKey operation-obtain MachineKey and machinekey

Source: Internet
Author: User

MachineKey operation-obtain MachineKey and machinekey
Introduction to obtaining MachineKey

Configure the MachineKey to encrypt and decrypt the Cookie data and view status data for Forms authentication, and use it to verify the non-process session status identity. This article describes how to obtain the unique MachineKey automatically generated by IIS for the application and the manually generated MachineKey. The following two methods are applicable, but there are some differences between them.

Generally, MachineKey cannot be obtained. (manually generated keys are not in this category and can be copied directly. The obtained keys are automatically generated by IIS and are historical projects .)FormsAuthenticationOr System. Web. Security. MachineKey and other related operation classes to obtain encryption for related operations. When Microsoft encapsulates the MachineKey, It is the access level of internal, and it is not recommended to obtain it.

 

I tried many methods, such as obtaining the APPCMD command, obtaining WMI, and so on, and finally had to code the implementation. If there are other methods, please reply to us for further instructions. This acquisition can only be performed using reflection.

Two methods for obtaining MachineKey

Method 1:

private string ConvertToHex(byte[] binary)    {        return binary.Aggregate(            new StringBuilder(),            (acc, c) => acc.AppendFormat("{0:x2}", c),            acc => acc.ToString());    }    string key = "", iv = "";    public void GetMachineKey()    {    System.Web.Configuration.MachineKeySection section = (System.Web.Configuration.MachineKeySection)        ConfigurationManager.GetSection("system.web/machineKey");    System.Reflection.BindingFlags flags =        System.Reflection.BindingFlags.Instance |        System.Reflection.BindingFlags.NonPublic |        System.Reflection.BindingFlags.GetProperty;    Func<string, byte[]> propertyReader = name => (byte[])section        .GetType()        .GetProperty(name, flags)        .GetValue(section, null);    key = ConvertToHex(propertyReader("DecryptionKeyInternal"));    iv = ConvertToHex(propertyReader("ValidationKeyInternal"));}

The disadvantage of this method is that it can only be obtained at the first initialization and the second retrieval, all of which are 00000000000000000000000000 byte arrays.

 

 

Method 2:

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");            MachineKeySection machineKeySection = (MachineKeySection)config.GetSection("system.web/machineKey");            PropertyInfo validata = ty.GetProperty("ValidationKeyInternal", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);            PropertyInfo desc = ty.GetProperty("DecryptionKeyInternal", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);               byte[] valiValue = (byte[])validata.GetValue(machineKeySection);            byte[] descValue = (byte[])desc.GetValue(machineKeySection);            string valiStr = null;            foreach (var item in valiValue)            {                valiStr += string.Format("{0:x2}", item);            }            string descStr = null;            foreach (var item in descValue)            {                descStr += string.Format("{0:x2}", item);            }

The only difference between this method is that the method for obtaining configuration is changed 【WebConfigurationManagerIn this way, any page or call can be obtained.

 

In this way, if an old project requires SSO integrated distributed development without stopping the service, you can directly obtain the value and modify the configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.