用asp.net自带Provider给web.config加密的注意事项
2008-12-09 14:45
393 查看
下面是加密的程序,但是在本机加密以后把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());
}
}
一:给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-webconfig中注意事项
- win10 ASP.NET VS2015 iis 本地发布Web HelloWorld 方法及注意事项
- ASP.NET应用程序中加密Web.config 转载
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- asp.net 2.0中加密web.config /App.config
- web.config文件加密-ASP.NET
- asp.net 2.0 中加密web.config 文件中的配置节
- 提高ASP.NET WEB程序的几点注意事项
- asp.net 2.0 中加密web.config 文件中的配置节
- 采用ASP.NET IIS 注册工具 (Aspnet_regiis.exe)对web.config实行本地加密
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET 2.0加密Web.config 配置文件
- asp.net Web.config加密解密
- 利用ASP.NET加密和解密Web.config中连接字符串
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- asp.net 2.0中加密web.config /App.config
- ASP.NET 程序安全性 (一) web.config加密与解密
- ASP.NET 程序安全性 (一) web.config加密与解密