How to get randomly generated cookie encryption and verification keys in ASP.NET

xiaoxiao2021-03-17  189

This article is

From ASP.NE T 1.1 to ASP.NET 2.0 Supplements of cookie issues to be considered, through example code, how to get reflex in ASP.NET 1.1 and ASP.NET 2.0 to get randomly generated cookie encryption and verification keys.

ASP.NET 1.1 sample code:

Object

MachineKeyConfig

=

HttpContext.current.getconfig

"

System.Web / MachineKey

"

);

//

Get the instance of System.Web.Configuration.machineKey MachineKeyConfig, MachineKeyConfig is the nested class of MachineKey

Type MachineKeyType

=

MachineKeyConfig.gettype (). assmbly.gettype

"

System.Web.configuration.MachineKey

"

);

//

Get the type of system.web.configuration.machineKey

BindingFlags BF

=

Bindingflags.nonpublic

|

Bindingflags.static;

//

Set the binding sign

MethodInfo ByteArraytohexString

=

MachineKeyType.getMethod

"

ByteaRrayToHexString

"

, BF);

//

ByteaRrayToHexString method in MachineKey is obtained by reflection, which is used to convert byte arrays to a string representative represented by

Byte [] ValidationKey

=

(Byte []) MachineKeytype.Getfield (

"

S_ValidationKey

"

, bf) .GetValue (MachineKeyConfig);

//

Get the verification key byte array

Symmetricalgorithm Algorithm

=

(Symmetricalgorithm) MachineKeytype.Getfield (

"

s_odes

"

, bf) .GetValue (MachineKeyConfig); Byte [] DecryptionKey

=

Algorithm.Key;

//

Get an encryption key byte array

String

ValidationKey

=

(

String

) byteaRrayToHexString.invoke

NULL

,

New

Object

[] {ValidationKey, ValidationKey.length});

//

Convert the verification key byte array to a string representative of 16

String

DecryptionKey

=

(

String

) byteaRrayToHexString.invoke

NULL

,

New

Object

[] {DecryptionKey, DecryptionKey.Length});

//

Convert the encryption key byte array to a string representative of 16

ASP.NET 2.0 sample code:

System.Web.configuration.MachineKeySection MachineKeysection

=

New

System.Web.configuration.machineKeySection ();

//

Create a MACHINEKEYSECTION instance, ASP.NET 2.0 replaces MachineKey in ASP.NET 1.1 in ASP.NET 2.0, and can be directly accessed and not protected by INTERNAL.

Type Type

=

Typeof

System.Web.configuration.machineKeySection;

//

Or MachineKeySecion.gettype ();

PropertyInfo PropertyInfo

=

TYPE.GETPROPERTY

"

ValidationKeyinternal

"

Bindingflags.nonpublic

|

Bindingflags.instance); byte [] ValidationKeyArray

=

(Byte []) PropertyInfo.getValue (MachineKeySection,

NULL

);

//

Get randomly generated verification key byte arrays

PropertyInfo

=

TYPE.GETPROPERTY

"

DecryptionKeyinternal

"

Bindingflags.nonpublic

|

Bindingflags.instance; byte [] DecryptionKeyArray

=

(Byte []) PropertyInfo.getValue (MachineKeySection,

NULL

);

//

Get randomly generated encryption key authentication arrays

MethodInfo ByteArraytohexString

=

TYPE.GETMETHOD

"

ByteaRrayToHexString

"

BindingFlags.Static

|

Bindingflags.nonpublic);

//

ByteaRrayToHexString method in MachineKeySection is obtained by reflection, the method is used to convert byte arrays to a string representative

String

ValidationKey

=

(

String

) byteaRrayToHexString.invoke

NULL

,

New

Object

[] {ValidationKeyArray, ValidationKeyArray.length});

//

Convert the verification key byte array to a string representative of 16

String

DecryptionKey

=

(

String

) byteaRrayToHexString.invoke

NULL

,

New

Object

[] {DecryptionKeyArray, DecryptionKeyArray.length});

//

Convert the encryption key byte array to a string representative of 16

//

Author Blog:

http://dudu.cnblogs.com

转载请注明原文地址:https://www.9cbs.com/read-129577.html

New Post(0)