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 );
}
}