Use the "machineKey" section in the web. config file to implement Single-point logon 2

Source: Internet
Author: User

This sample code is not encrypted and will not be published to the server. For security considerations, the <machineKey> encryption published to the server is very important. You can see the encrypted <machineKey> in Listing 2.
List 2: Encrypted machineKey in web. config

<MachineKeyconfigProtectionProvider = "RsaProtectedConfigurationProvider">
<EncryptedDataType = "http://www.w3.org/2001/04/xmlenc#Element"
Xmlns = "http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethodAlgorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<KeyInfoxmlns = "http://www.w3.org/2000/09/xmldsig#">
<EncryptedKeyxmlns = "http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethodAlgorithm = "http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<KeyInfoxmlns = "http://www.w3.org/2000/09/xmldsig#">
<KeyName> Rsa Key </KeyName>
</KeyInfo>
<CipherData>
<CipherValue>
Lm3mfPX/94Zm3HgdbsmKiIxbrWM14t3/ugxs40BFOAHbIaCtwQ3gVQusFtOFVUoNVny01kgBCeh10rVEId
DjNZ/8luBNoCbHm8OLjgPLHVrT + rjc/LRpESJk2ni/Jy2sWKXlgejgSQ1W5NE53GZtG3s9hu + nk4owxnt
6z3v7AM =
</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>
BCEGUV/dh1Imbcm5vn0Kn8NrD + EX + KemenR7x + VekwT1ZO6y5 + jRyF4RDWMJCfJ1jHC36 + MAfCdHuXN0rP
B6hu5YUtX9VA5q5N0NGrs9AIpG + 0ihuuS3HDzQe3P6nlI30m1h0pmL1yJBovY0i6fbCA6 + + GT2MdwCLERk
+ PVWmoq7p1q97n5pNzNqhVKCX45lhS5ySVS + MjJXVeTrcatftpvaUcjLsNcL2kMerzf5w/SU3AbLEuY04w
Pipeline +
0 CEFE/Hj2ChpYw =
</CipherValue>
</CipherData>
</EncryptedData>
</MachineKey>

You can encrypt your Configuration files through the Configuration and SectionInformation classes. To encrypt and decrypt your <machineKey>, let's write some code. The SectionInformation class has a method ProtectSection (). You can obtain a string describing the Protection Provider, such as "RSAProctedConfigurationProvider", and encrypt this configuration section. There is also a Boolean type attribute ForceSave, which needs to be set to true when the save method of the configuration class is required to save the configuration file. The Code on the "Encryption. aspx" page contains two buttons to encrypt and decrypt the configuration file.

Listing 3: web configuration file encryption code
Protected void btnEncrypt_Click (object sender, EventArgs e)
{
Try
{
Configuration config = WebConfigurationManager. OpenWebConfiguration (
"/Aspalliance1 ");
ConfigurationSection machineKeySection = config. GetSection (
"System. web/machineKey ");
MachineKeySection. SectionInformation. ProtectSection (
"RSAProtectedConfigurationProvider ");
MachineKeySection. SectionInformation. ForceSave = true;
Config. Save ();
Response. Write ("

}
Catch (Exception ex)
{
Response. Write ("

Response. Write (ex. Message );
}
}

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.