用asp.net自带Provider给web.config加密的注意事项
来源:互联网 发布:mysql数据库恢复命令 编辑:程序博客网 时间:2024/06/05 22:49
下面是加密的程序,但是在本机加密以后把web.config传到服务器上以后会出现未能使用提供程序“RsaProtectedConfigurationProvider”进行解密 的错误信息。其实解决的办法很简单。只要把加密的程序一并传到服务器上。然后运行一下。这样就好用了(不过运行加密程序前,web.config应该是没加密的哦)。这个加密程序代码来自圣殿祭司的asp.net2.0开发详解
一:给connectionstrings加密
Configuration myConfiguration = null;
ConfigurationSection myConnectionStrings = null;
protected void Page_Load(object sender, EventArgs e)
{
}
//以DPAPIProtectedConfigurationProvider加密
protected void btnDPAIP_Click(object sender, EventArgs e)
{
try
{
getConnectionSettings(out myConfiguration, out myConnectionStrings);
if (!myConnectionStrings.SectionInformation.IsProtected)
{
//DPAPI加密
myConnectionStrings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
myConfiguration.Save(); //存储设置写入web.config文件
new AlertMessage().showMsg(this.Page, "以DPAIP加密成功!");
}
}
catch (Exception ex)
{
new AlertMessage().showMsg(this.Page,ex.Message.ToString());
}
}
//以RSAProtectedConfigurationProvider加密
protected void btnRSA_Click(object sender, EventArgs e)
{
try
{
getConnectionSettings(out myConfiguration, out myConnectionStrings);
if (!myConnectionStrings.SectionInformation.IsProtected)
{
//RSA加密
myConnectionStrings.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
myConfiguration.Save(); //存储设置写入web.config文件
new AlertMessage().showMsg(this.Page, "以RSA加密成功!");
}
}
catch (Exception ex)
{
new AlertMessage().showMsg(this.Page,ex.Message.ToString());
}
}
//取得取得Web.config中connectionStrings设置区块
protected void getConnectionSettings(out Configuration myConfig,out ConfigurationSection connStrings)
{
//打开Request所在路径网站的Web.config文件
myConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//取得Web.config中connectionStrings设置区块
connStrings = myConfig.GetSection("connectionStrings");
}
//解密
protected void btnDecrypt_Click(object sender, EventArgs e)
{
try
{
getConnectionSettings(out myConfiguration, out myConnectionStrings);
myConnectionStrings.SectionInformation.UnprotectSection(); //解密
myConfiguration.Save();
new AlertMessage().showMsg(this.Page, "connectionStrings解密成功!");
}
catch (Exception ex)
{
new AlertMessage().showMsg(this.Page,ex.Message.ToString());
}
}
二给appsettings加密
Configuration myConfiguration = null;
ConfigurationSection myAppSettings = null;
protected void Page_Load(object sender, EventArgs e)
{
}
//以DPAPIProtectedConfigurationProvider加密
protected void btnDPAIP_Click(object sender, EventArgs e)
{
try
{
getAppSettings(out myConfiguration, out myAppSettings);
if (!myAppSettings.SectionInformation.IsProtected)
{
//DPAPI加密
myAppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
myConfiguration.Save(); //存储设置写入web.config文件
new AlertMessage().showMsg(this.Page, "以DPAIP加密成功!");
}
}
catch (Exception ex)
{
new AlertMessage().showMsg(this.Page,ex.Message.ToString());
}
}
//以RSAProtectedConfigurationProvider加密
protected void btnRSA_Click(object sender, EventArgs e)
{
try
{
getAppSettings(out myConfiguration, out myAppSettings);
if (!myAppSettings.SectionInformation.IsProtected)
{
//RSA加密
myAppSettings.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
myConfiguration.Save(); //存储设置写入web.config文件
new AlertMessage().showMsg(this.Page, "以RSA加密成功!");
}
}
catch (Exception ex)
{
new AlertMessage().showMsg(this.Page,ex.Message.ToString());
}
}
//取得Web.config中appSettings设置区块
protected void getAppSettings(out Configuration myConfig,out ConfigurationSection appSettings)
{
//开启Request所在路径网站的Web.config文件
myConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//取得Web.config中appSettings设置区块
appSettings = myConfig.GetSection("appSettings");
}
//解密
protected void btnDecrypt_Click(object sender, EventArgs e)
{
try
{
getAppSettings(out myConfiguration, out myAppSettings);
if (myAppSettings.SectionInformation.IsProtected)
{
myAppSettings.SectionInformation.UnprotectSection(); //解密
myConfiguration.Save();
}
new AlertMessage().showMsg(this.Page, "appSettings解密成功!");
}
catch (Exception ex)
{
new AlertMessage().showMsg(this.Page,ex.Message.ToString());
}
}
- 用asp.net自带Provider给web.config加密的注意事项
- ASP.NET的Web.config文件 的加密方法
- asp.net 2.0中加密web.config
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- Asp.Net 2.0加密Web.config配置文件
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- asp.net 2.0中加密web.config
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- web.config文件加密-ASP.NET
- ASP.NET加密Web.config 配置文件
- ASP.NET 2.0加密Web.config文件
- Asp.net实现无刷新检测用户名
- 正则表达式的基本语法
- 金融危机&&经济危机与全球的裁员风暴
- 目录选择对话框
- 写程序经验总结 ----(本文献给天津理工大学计算机学院的学弟学妹们~~)
- 用asp.net自带Provider给web.config加密的注意事项
- COBOL语言
- CMap类的使用
- reborn
- 长尾管理软件市场怎么做-我看E-CELL
- 策略类模式-学习笔记
- 一个通用的验证类的探索
- MS:Who we look for
- obstart() 几个函数的简单使用方法