您的位置:首页 > 编程语言 > ASP

ASP.NET 2.0加密Web.config 配置文件 (终极版)

2008-02-05 10:08 751 查看
概述:
使用受保护配置来加密Web 应用程序配置文件(如 Web.config 文件)中的敏感信息(包括用户名和密码、数据库连接字符串和加密密钥)。对配置信息行加密后,即使攻击者获取了对配置文件的访问,也可以使攻击者难以获取对敏感信息的访问,从而改进应用程序的安全性。

使用方法:
在asp.net2.0中新增了对web.config中的部分数据进行加密的功能,可以使用 RSAProtectedConfigurationProvider和DPAPIProtectedConfigurationProvider来加 密,本文说明使用RSAProtectedConfigurationProvidert和计算机级别的密钥容器进行加密的步骤。

加密前:


<connectionStrings>


<add name="Pubs" connectionString="Server=localhost;Integrated Security=True;Database=Pubs"


providerName="System.Data.SqlClient" />


<add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind"


providerName="System.Data.SqlClient" />


</connectionStrings>

我们要做到加密后是:


<connectionStrings>


<EncryptedData>


<CipherData>


<CipherValue>AQAAANCMndjHoAw...</CipherValue>


</CipherData>


</EncryptedData>


</connectionStrings>

这里有两种方法:
1. 使用默认的RSA密钥容器。
2. 客户化自己的RSA密钥容器,不过这里就要再设置安全访问权限。

先介绍默认的怎么做:
1、打开记事本,然后将下面的代码复制到一个新文件中。


<%@ Page Language="C#" %>


<%


Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);


%>

保存 my.aspx 到你的web目录 ,运行一下窗体显示 “ NT AUTHORITY/NETWORK SERVICE ”

2. (关键一步)运行cmd,执行以下

cd %windows%/Microsoft.NET/Framework/versionNumber
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITYNETWORK SERVICE"

说明:注册默认的 RsaProtectedConfigurationProvider 的RSA 密钥容器,
NetFrameworkConfigurationKey 是 RsaProtectedConfigurationProvider 的默认provider。

3. 现在,可以加密web.config ,运行:
加密:


aspnet_regiis -pe "connectionStrings" -app "/Myweb"

说明:"connectionStrings" 是要加密的节,"/Myweb"是的web目录
解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb"

4. 这样就可以在程序里调用了(不用解密)


...
string connstr= ConfigurationManager.ConnectionStrings["myConnstr"].ConnectionString.ToString();
...

也可以用创建自己的RSA 密钥容器,如下:
1. 创建 "MyKeys" 密钥容器,运行:


aspnet_regiis -pc "MyKeys" -exp

2. 在web.config里加入以下代码:


<protectedData>


<providers>


<add name="MyProvider"


type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0. 0.0,


Culture=neutral, PublicKeyToken=b03f5f7f11d0a3a,


processorArchitecture=MSIL"


keyContainerName="MyKeys"


useMachineContainer="true" />


</providers>


</protectedData>

保存web.config

3. 授予帐户对计算机级别的 "MyKeys" RSA 密钥容器的访问权限,运行:


aspnet_regiis -pa "MyKeys" "NT AUTHORITYNETWORK SERVICE"

4. 现在,可以加密web.config ,运行:
加密:


aspnet_regiis -pe "connectionStrings" -app "/Myweb" -prov "MyProvider"

说明:"connectionStrings" 是要加密的节,"/Myweb"是的web目录,"MyProvider" 自己密钥容器
解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb" -prov "MyProvider"

这样就OK了!
--------------------------

如果要对配置节进行解密。此参数采用下面的可选参数:

· -app virtualPath 指定应该在包含路径的级别进行解密。
· -location subPath 指定要解密的子目录。
· -pkm 指定应该对 Machine.config 而非 Web.config 文件进行解密。
· -pdf section webApplicationDirectory 对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行解密。
· -pe section

对指定的配置节进行加密。此参数采用下面的可选修饰符:

· -prov provider 指定要使用的加密提供程序。
· -app virtualPath 指定应该在包含路径的级别进行加密。
· -location subPath 指定要加密的子目录。
· -pkm 指定应该对 Machine.config 而非 Web.config 文件进行加密。
· -pef section webApplicationDirectory 对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行加密。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: