首先添加引用: using system.web.configuration;
加密操作如下:
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();
}
}
解密操作如下:
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();
}
}
实践:
加密前的配置文件:
<?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>
加密后的配置文件:
<?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
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器