![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
protected void Page_Load(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(this.Request.ApplicationPath);
MailSettingsSectionGroup smtp = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
Response.Write(smtp.Smtp.Network.Host);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
getAppSettings(out myConfiguration, out myAppSettings);
if (!myAppSettings.SectionInformation.IsProtected)
{
myAppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
myConfiguration.Save();
Response.Write("success jiami");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void getAppSettings(out Configuration myconfig, out ConfigurationSection appSettings)
{
myconfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
appSettings = myconfig.GetSection("appSettings");
}
protected void Button3_Click(object sender, EventArgs e)
{
try
{
getAppSettings(out myConfiguration, out myAppSettings);
if (myAppSettings.SectionInformation.IsProtected)
{
myAppSettings.SectionInformation.UnprotectSection();
myConfiguration.Save();
}
Response.Write("success jiemim");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
以上就可以对appsettings的区块内容加密了,使用"DataProtectionConfigurationProvider"进行DPAPI加密
用"RSAProtectionConfigurationProvider"就可以进行RSA加密了,只要改改provider就可以了。
用简单的UnprotectSection()就可以解密了
如果对数据库连接字符串部分加密可以用
myConfig=GetSection("connectionStrings");
就可以了
用aspnet_regiis -pe(加密,解密用-pd) “connectionStrings" -app "虚拟目录,在c:\intpub\wwwroot中" -prov "加密方式"