First add a reference: using System.Web.Configuration;
The encryption operation is as follows:
| The following are the referenced contents:
private void ProtectSection (String sectionname, string provider)
{
Configuration config =
WebConfigurationManager.
Openwebconfiguration (Request.applicationpath);
ConfigurationSection section = Config. GetSection (sectionname);
if (section!= null &&!section. sectioninformation.isprotected)
{
Section. Sectioninformation.protectsection (provider);
Config. Save ();
}
}
|
The decryption operation is as follows:
| The following are the referenced contents:
private void Unprotectsection (String sectionname)
{
Configuration config =
WebConfigurationManager.
Openwebconfiguration (Request.applicationpath);
ConfigurationSection section = Config. GetSection (sectionname);
if (section!= null && section. sectioninformation.isprotected)
{
Section. Sectioninformation.unprotectsection ();
Config. Save ();
}
}
|
Practice:
Configuration file before encryption:
The following are the referenced contents:
<?xml version= "1.0"?>
<configuration>
<appSettings>
<add key= "name" value= "shy520"/>
<add key= "Address" value= "Cnblogs"/>
</appSettings>
<system.web>
<compilation debug= "true"/>
</system.web>
</configuration>
Encrypted configuration file:
<?xml version= "1.0"?>
<configuration>
<appsettings configprotectionprovider= "DataProtectionConfigurationProvider" >
<EncryptedData>
<CipherData>
<CipherValue>
Aqaaancmnd8bfderjhoawe/cl+s
Baaaabi1atlnkeugef0xywgl2xg
Qaaaacaaaaaaadzgaaqaaaabaaa
Abihxmwlazantwiipst1cdxaaaa
aasaaacgaaaaeaaaapz/ykyx07c
B+h4fqdr4fklgaaaax1ieyc+wsx
Afsdw1vn2c/fxsg2tanyeugacov
8e3ngfthhsh91gliqkregfpyzgr
Vw1xrez/3vwomji9es7efkrcxej
Nnhl66kg2inrk3ntlnzlztts3cz
9w63u47vkajs6miwsgbz2gntl/9
Ughleligrjcr3yj+lsjoscexqnv
Hgva48efxpd+teifbtgxehsfkqx
Cqgysshx16vct2gunuc3zmeahbh
Usafkpiyqelyhd4+m9a/xpe2tqw
Gibla1wbw2ndefrzjpwnkfmpnqr
hxijkimipwxbdvyy6o0uaaaas8d
suynohj7qajja2c/4euc7sks=
</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
<system.web>
<compilation debug= "true"/>
</system.web>
</configuration> |