asp.net加密web.config 加密数据库连接字符串
2012-08-02 09:33
731 查看
加密web.config的appSettings节点和connectionStrings节点
新建Default.aspx;Page_Load事件如下protected void Page_Load(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");//获取网站根目录下的配置文件
ConfigurationSection appSetting = config.GetSection("appSettings");//获取appSettings配置块信息
if (appSetting.SectionInformation.IsProtected)
{//判断是否已经加密,如果已经加密则进行解密
appSetting.SectionInformation.UnprotectSection();
}
else
{//如果没有加密则进行加密
appSetting.SectionInformation.ProtectSection("DataprotectionConfigurationProvider");
}
config.Save();
Response.Write("读取webconfig文件中appSettings配置节点<br /><br />");
foreach (string key in WebConfigurationManager.AppSettings.Keys)
{
Response.Write(key+"值:");
Response.Write( WebConfigurationManager.AppSettings[key] + "<br />");
}
}
查看配置文件;发现appSettings节点已经加密;
加密connectionStrings节点
protected void Page_Load(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");//获取网站根目录下的配置文件
ConfigurationSection appSetting = config.GetSection("connectionStrings");//获取connectionStrings配置块信息
if (appSetting.SectionInformation.IsProtected)
{//判断是否已经加密,如果已经加密则进行解密
appSetting.SectionInformation.UnprotectSection();
}
else
{//如果没有加密则进行加密
appSetting.SectionInformation.ProtectSection("DataprotectionConfigurationProvider");
}
config.Save();
}
加密配置文件;此方法使用一次即可加密
用户访问appSettings节点或connectionStrings节点时会asp.net会自动解密无需再使用代码解密
相关文章推荐
- ASP.NET web.config中,数据库连接字符串的加密与解密
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- (译)利用ASP.NET加密和解密Web.config中连接字符串
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- asp.net 2.0 中使用web.config存储数据库连接字符串
- Asp.net中Web.config连接字符串及配置数据库sqlserver
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中(1)
- 利用ASP.NET加密和解密Web.config中连接字符串
- 利用ASP.NET加密和解密Web.config中连接字符串
- ASP.NET SqlDataSource中使用web.config 共享数据库连接字符串(详细)
- ASP.NET中web.config中加密连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中
- 【ASP.NET】将数据库连接字符串写在Web.config
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET中web.config中加密连接字符串 [转自9who]