使用受保护配置来加密 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);
%>
2. (关键一步)运行cmd,执行以下
aspnet_regiis - pa " NetFrameworkConfigurationKey " " NT AUTHORITYNETWORK SERVICE "
NetFrameworkConfigurationKey 是 RsaProtectedConfigurationProvider 的默认provider。
3. 现在,可以加密web.config ,运行:
加密:
aspnet_regiis
-
pe
"
connectionStrings
"
-
app
"
/Myweb
"
解密: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
>
3. 授予帐户对计算机级别的 "MyKeys" RSA 密钥容器的访问权限,运行:
aspnet_regiis
-
pa
"
MyKeys
"
"
NT AUTHORITYNETWORK SERVICE
"
4. 现在,可以加密web.config ,运行:
加密:
aspnet_regiis
-
pe
"
connectionStrings
"
-
app
"
/Myweb
"
-
prov
"
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 文件的指定配置节进行加密。

本文介绍如何使用ASP.NET内置工具aspnet_regiis和RSAProtectedConfigurationProvider来加密Web.config中的敏感信息,例如数据库连接字符串,以增强Web应用安全性。
210

被折叠的 条评论
为什么被折叠?



