这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串
在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Server, Ajax, JavaScript等的文章。大多数情况下,我都把数据库的连接字符串放在了web.config中。其中包含许多敏感信息,包括连接数据库的用户名密码等。然而我们在web.config和machine.config中以纯文本的方式保存密码安全吗?
如果我们的程序只是部署在内部服务器中,这应该没什么问题。但如果我们的程序是运行在共享主机上面,那我们应该提高安全等级了。ASP. NET 2.0提供了一个保护配置模型来加密和解密web.config中sections信息。RSAProtectedConfigurationProvider:默认通过RSA公钥来加密和解密。
通过在命令行中工具运行aspnet_regiis.exe命令,可以对web.config中的连接串进行加密和解密。
首先,我们通过在windows命令行中执行aspnet_regiis.exe来加密与解密。
在VS中创建一个新的websit项目,打开web.config,加入数据库连接串,如:
然后我们按下面的步骤来加密和解密数据连接串
1. 开始菜单>>所有程序>>Microsoft visual studio 2008 >> Visual Studio Tools >> Visual Studio 2008 开发人员命令提示(如果是windows7,点右键与管理员身份运行)
2. 在命令窗口中,输入命令 aspnet_regiis.exe -pef "connectionStrings" "C:\VisualStudio2008\Authorization"
–pef表明程序是以文件系统的形式建立的。第二个“connectionStrings”是你要加密的configuration 节点名字。第三个参数指名 web.config的物理路径。
3. 成功执行命令后会显示:加密成功。
现在,再打开程序中的 web.config,会变成像下面这样子了。
我们在程序中并不要写任何代码来解密连接字符串,因为.NET会自动的为我们解密。如果我们要用连接字符串,可以像平常那样调用.
string strconnection = ConfigurationManager.AppSettings["dbconnection"].ToString();
如果我们想解密,只需要在VS的命令窗口中,输入aspnet_regiis.exe -pdf "connectionStrings" "C:\VisualStudio2008\Authorization"
成功执行后,会显示解密成功。
再打开web.config,我们可以看到解密后的字符串。
现在,我们知道了如何在文件系统中加密和解密连接字符串。如果我们想加密运行在IIS上的默认网站,就像IE上展示的那样,可以用下面的命令。
加密IIS默认网站的web.config
aspnet_regiis.exe -pe "connectionStrings" -app "/SampleWebSite"
-pe说明程序是运行在IIS上的。第二个参数指名要加密的configuration节点。-app用来指定虚拟目录,最后一个参数就是程序部署的虚拟目录名。
Decrypt connectionStrings in web.config of IIS based site
解密IIS默认网站上的web.config
aspnet_regiis.exe -pd "connectionStrings" -app "/SampleWebSite"
到这里我们知道如何用命令行工具执行aspnet_regiis.exe命令来加密和解密web.config了。下面我将介绍如何在后台代码中来加密解密web.config。
在第二种方法中我会用RSAProtectedConfigurationProvider和DataProtectionConfgurationProvider来加密解密web.config
首先,打开Default.aspx,添加如下代码:
打开后台代码,添加下列命名空间:
using System;
using System.Configuration;
using System.Web.Configuration;
再添加如下代码
string provider = "RSAProtectedConfigurationProvider";
string section = "connectionStrings";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnEncrypt_Click(object sender, EventArgs e)
{
Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSect = confg.GetSection(section);
if (configSect != null)
{
configSect.SectionInformation.ProtectSection(provider);
confg.Save();
}
}
protected void btnDecrypt_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection configSect = config.GetSection(section);
if (configSect.SectionInformation.IsProtected)
{
configSect.SectionInformation.UnprotectSection();
config.Save();
}
}
完成之后,打开web.config,添加数据库连接字符串
现在运行程序并点击加密按钮之后,再打开web.config,会变成下面那样:
如果我们想用DataProtectionConfigurationProvider来实现加密与解密,只需在代码中将RSAProtectedConfigurationProvider替换成DataProtectionConfigurationProvider即可。
原文:http://www.codeproject.com/Tips/304638/Encrypt-or-Decrypt-Connection-Strings-in-web-confi
手机扫一扫
移动阅读更方便
你可能感兴趣的文章