1. Web. config settings
< Machinekey Validationkey = "Dd305f238c8f26103323f10b1a655ed9fa7dbb7a" Decryptionkey = "70de128000e565a7f8441a4c091c11ec8c5bb9b4d4eda7a1" Validation = "Sha1" />
< Authentication Mode = "Forms" >
< Forms Name = "Skapp. aspxauth" Loginurl = "~ /Login. aspx" Protection = "All" Timeout = "10" Path = "/" />
</ Authentication >
1,AuthenticationSection
Purpose: Configure ASP. NET authentication support (Windows, forms, passport, none ). This element can only be used on computers, sites, or applications.ProgramLevel statement. The <authentication> element must be used with the <authorization> section.
Example:
In the following example, the website is configured for form-based authentication. When a user who does not log on to the website that requires authentication, the webpage automatically jumps to the logon webpage.
<Authentication mode = "forms">
<Forms loginurl = "Logon. aspx" name = ". formsauthcookie"/>
</Authentication>
The element loginurl indicates the name of the login webpage, and name indicates the cookie name.
2,Machinekey
Purpose: The key is configured to encrypt and decrypt the cookie data and view status data for Forms authentication, and used to verify the non-process session status identity.
Attribute
| Attribute |
Description |
|
Decryption |
Optional string attributes.
Hash used to decrypt dataAlgorithm.
This attribute can be one of the following values.
| Value |
Description |
|
Auto |
Specify the configuration-based default settings of ASP. NET to determine which decryption algorithm to use. This is the default value. |
|
AES |
Specify that ASP. NET uses the AES (Rijndael) algorithm to decrypt data.AESIs the default algorithm used to decrypt data. |
|
3DES |
Specify ASP. NET to use the tripledes algorithm to decrypt data. Tripledes (3DES) The algorithm uses the three consecutive iterations of the DES algorithm. |
This attribute is a new attribute in. NET Framework 2.0.
The default value is"Auto". |
|
Decryptionkey |
RequiredStringAttribute.
Specifies the key used to encrypt and decrypt data or the process that generates the key. WhenValidationSetTripledesThis attribute is used for Forms authentication encryption and decryption, and view State encryption.
If you addIsolateappsModifier, ASP. NET will use the Application ID of each application to generate a unique encryption key for each application.IsolateappsIt is also part of the default value.
If you need to support configuration in the web server network (Network farm), manually set this attribute to ensure that the configuration is consistent.
This attribute can be one of the following values.
| Value |
Description |
|
Autogenerate |
Specify ASP. NET to generate a random key and store it in LSA. This value is the default value. IfDecryptionkeyAdd ValueIsolateappsModifier, ASP. NET will use the Application ID of each application to generate a unique encryption key for each application. |
|
Value |
Specify a manually assigned key. This value must be manually set to a hexadecimal string to ensure that the configuration is consistent throughout the network. When des is used for encryption, the key length should be 16 characters; When des (3 DES) is used for encryption, the key length should be 48 characters. If you want to use a key with a length less than the maximum length, you should create these keys in a real random way (for example, by using the rngcryptoserviceprovider class. ASP. NET can use Triple DES only when the computer uses 128-bit encryption. IfDecryptionkeyAdd ValueIsolateappsModifier, ASP. NET will use the Application ID of each application to generate a unique encryption key for each application. |
The default value is"Autogenerate, isolateapps".
|
|
Validation |
mandatory machinekeyvalidation attribute.
specifies the encryption type used to verify the data.
This attribute can be one of the following values.
| value |
description |
|
AES |
specify ASP. NET to use the AES (Rijndael) algorithm to decrypt data. AES is the default algorithm used to decrypt data. |
|
MD5 |
specify that ASP. NET uses the message digest 5 (MD5) hash algorithm. This algorithm performs better than sha1. |
|
sha1 |
specify that ASP. NET uses the sha1 hash algorithm. This algorithm enhances security.
this value is the default value. |
|
tripledes |
specify ASP. NET to use the tripledes algorithm to decrypt data. The tripledes algorithm uses three consecutive iterations of the DES algorithm.
in. NET Framework 1.0 and 1.1, the value is "3DES" . |
the default value is "sha1" . |
|
Validationkey |
RequiredStringAttribute.
Specifies the key used to verify encrypted data. When enableviewstatemac isTrue,ValidationkeyUsed to create Message AuthenticationCode(Mac) to ensure that the view status is not tampered.ValidationkeyIt can also be used to generate out-of-process and application-specific session IDs to ensure that each session has an independent session state variable.
If you add a value to the validationkey ValueIsolateappsModifier, ASP. NET will use the Application ID of each application to generate a unique encryption key for each application. Isolateapps are also part of the default value.
If you need to support configuration in the web server network (Network farm), manually set the validationkey attribute to ensure that the configuration is consistent.
ValidationkeyAttribute can be one of the following values.
| Value |
Description |
|
Autogenerate |
Specify ASP. NET to generate a random key and store it in LSA. This option is the default value. IfValidationkeyAdd ValueIsolateappsModifier, ASP. NET will use the Application ID of each application to generate a unique encryption key for each application. |
|
Value |
Specify a manually assigned key. This value must be manually set to a hexadecimal string to ensure that the configuration is consistent throughout the network. When des is used for encryption, the key length should be 16 characters; When des is used for triple encryption, the key length should be 48 characters. If you want to use a key with a length less than the maximum length, you should create these keys in a real random way (for example, by using the cryptographyrngcryptoserviceprovider class. ASP. NET can use Triple DES only when the computer uses 128-bit encryption. IfValidationkeyAdd ValueIsolateappsModifier, ASP. NET will use the Application ID of each application to generate a unique encryption key for each application. |
The default value is"Autogenerate, isolateapps".
|
Ii. record user information
Code
Formsauthenticationticket ticket = New Formsauthenticationticket (
1 , // Ticket version number
Memberid + " ; " + Txtmemberno. Text, // The user information to be recorded, separated ";"
Datetime. Now, // User information record time
Datetime. Now. addminutes ( 30 ), // User Information expiration time
True , // Use cookies to record user information
Dr [ " Roles " ]. Tostring (), // Other user information that is written to the record information. The user permission is recorded here.
Formsauthentication. formscookiepath // Cookie writing path
);
String Hash = Formsauthentication. Encrypt (ticket ); // Encrypt user information to write cookies
Httpcookie = New Httpcookie (
Formsauthentication. formscookiename, // Name of the cookie to be written
Hash // Encrypted user information
);
If (Ticket. ispersistent) // If the user information is persistent
Cookie. Expires = Ticket. expiration; // Cookie writing expiration date
Response. Cookies. Add (cookie ); // Write cookies
3. Read user information
Code
If (Httpcontext. Current. User. Identity. isauthenticated) // Whether the user passes Verification
{
String [] Parts = Httpcontext. Current. User. Identity. Name. Split ( ' ; ' ); // Returns the recorded user information.
Return Convert. toint32 (parts [ 0 ]);
}
Else
Return Null ;