How to obtain random cookie encryption and verification keys in ASP. NET

Source: Internet
Author: User


This article is a supplement to the cookie issue that needs to be considered when upgrading from ASP. ne t 1.1 to ASP. NET 2.0. Code This section describes how to obtain random cookie encryption and verification keys in ASP. NET 1.1 and ASP. NET 2.0 through reflection.
Sample Code for ASP. NET 1.1: Object Machinekeyconfig = Httpcontext. Current. getconfig ( " System. Web/machinekey " );
// Obtain the instance of system. Web. configuration. machinekey + machinekeyconfig. machinekeyconfig is a nested class of machinekey.

Type machinekeytype = Machinekeyconfig. GetType (). Assembly. GetType ( " System. Web. configuration. machinekey " );
// Obtain the system. Web. configuration. machinekey type.

Bindingflags BF = Bindingflags. nonpublic | Bindingflags. Static;
// Set binding flag

Methodinfo bytearraytohexstring = Machinekeytype. getmethod ( " Bytearraytohexstring " , BF );
// Obtain the bytearraytohexstring method in the machinekey through reflection. This method is used to convert a byte array to a hex string.

Byte [] validationkey = (Byte []) machinekeytype. getfield ( " S_validationkey " , BF). getvalue (machinekeyconfig );
// Obtain the verification key byte array
Symmetricalgorithm Algorithm = (Symmetricalgorithm) machinekeytype. getfield ( " S_odes " , BF). getvalue (machinekeyconfig );
Byte [] decryptionkey = Algorithm. Key;
// Obtain the Encrypted Key byte array
String Validationkey = ( String ) Bytearraytohexstring. Invoke ( Null , New   Object [] {Validationkey, validationkey. Length });
// Converts the verification key byte array to a hex string.
String Decryptionkey = ( String ) Bytearraytohexstring. Invoke ( Null , New   Object [] {Decryptionkey, decryptionkey. Length });
// Converts an encrypted key byte array to a hexadecimal string.

Sample Code for ASP. NET 2.0:System. Web. configuration. machinekeysection =   New System. Web. configuration. machinekeysection ();
// Directly create an instance of machinekeysection. in ASP. NET 2.0, use machinekeysection to replace the machinekey in ASP. NET 1.1, which can be accessed directly without being protected by internal.
Type type =   Typeof (System. Web. configuration. machinekeysection ); // Or machinekeysection. GetType ();

Propertyinfo = Type. getproperty ( " Validationkeyinternal " , Bindingflags. nonpublic | Bindingflags. instance );
Byte [] validationkeyarray = (Byte []) propertyinfo. getvalue (machinekeysection, Null );
// Obtain the randomly generated verification key byte array

Propertyinfo = Type. getproperty ( " Decryptionkeyinternal " , Bindingflags. nonpublic | Bindingflags. instance );
Byte [] decryptionkeyarray = (Byte []) propertyinfo. getvalue (machinekeysection, Null );
// Obtain a random array of encrypted key bytes

Methodinfo bytearraytohexstring = Type. getmethod ( " Bytearraytohexstring " , Bindingflags. Static | Bindingflags. nonpublic );
// Obtain the bytearraytohexstring method in machinekeysection through reflection. This method is used to convert a byte array to a hex string.
String Validationkey = ( String ) Bytearraytohexstring. Invoke ( Null , New   Object [] {Validationkeyarray, validationkeyarray. Length });
// Converts the verification key byte array to a hex string.
String Decryptionkey = ( String ) Bytearraytohexstring. Invoke ( Null , New   Object [] {Decryptionkeyarray, decryptionkeyarray. Length });
// Converts an encrypted key byte array to a hexadecimal string.

//Author's blog:Http://dudu.cnblogs.com

Related Article

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.