asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

加密和解密ASP.NET配置文件(Web.config)


发布日期:2018年11月24日
 
加密和解密ASP.NET配置文件(Web.config)

首先添加引用:usingSystemWebConfiguration;

加密操作如下:
PRivatevoidProtectSection(stringsectionNamestringprovider)
{
Configurationconfig=
WebConfigurationManager
OpenWebConfiguration(RequestapplicationPath);

ConfigurationSectionsection=configGetSection(sectionName);

if(section!=null&&!sectionSectionInformationIsProtected)
{
sectionSectionInformationProtectSection(provider);
configSave();
}
}

解密操作如下:
privatevoidUnProtectSection(stringsectionName)
{
Configurationconfig=
WebConfigurationManager
OpenWebConfiguration(RequestApplicationPath);

ConfigurationSectionsection=configGetSection(sectionName);

if(section!=null&&sectionSectionInformationIsProtected)
{
sectionSectionInformationUnprotectSection();
configSave();
}
}
实践:
加密前的配置文件:
<?xmlversion=""?>
<configuration>
<appSettings>
<addkey="name"value="shy"/>
<addkey="address"value="cnblogs"/>
</appSettings>
<systemweb>
<compilationdebug="true"/>
</systemweb>
</configuration>

加密后的配置文件(Webconfig):
<?xmlversion=""?>
<configuration>
<appSettingsconfigProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>
AQAAANCMndBFdERjHoAwE/Cl+s
BAAAABiATlNkEUGEfXyWGLXg
QAAAACAAAAAAADZgAAqAAAABAAA
ABIhxMWlazAntwIIpSTCDXAAAA
AASAAACgAAAAEAAAAPz/YKYxc
b+hfqdrfkLgAAAAXIeyc+WSx
AfsDWvnC/fXsGTAnYeUgaCov
enGFTHHsHgLiqKregFPYzGR
vWxrez/VwOmJIeSEFKrcXej
NnHLkgiNRkntLNZlZtTscZ
wuVKAjsmiWsGBzGntL/
UGHLELigrJcrYJ+lsjOscExQnv
HGvAEfxpD+tEiFBtgXeHsFkQX
cqGySshxvCtGUNUCZmEAhBh
UsAFkPIYqelYHd+ma/xPetqw
GIblawbWNDEfrzJPwnkfmpNqR
hXijKImipwXbDVYyoUAAAAsD
suYNOhJqAjJac/eUCsks=
</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
<systemweb>
<compilationdebug="true"/>
</systemweb>
</configuration>

上一篇:asp.net 连接Oracle数据库

下一篇:ASP.NET中几种加密方法