确保计算机上已经安装了IIS,并且建立的ASP.NEt网站.
(1)授予应用程序对 RSA 加密密钥的读取权限.因为ASP.NET 应用程序必须能读取用于加密的密钥,才能对 Web.config 文件中的已加密信息进行解密。Machine.config 文件中缺省指定了一个 提供程序, .. name="RsaProtectedConfigurationProvider" keyContainerName="NetFrameworkConfigurationKey" ..,即默认 RsaProtectedConfigurationProvider 提供程序使用的 RSA 密钥容器名为 "NetFrameworkConfigurationKey"。
下面取得ASP.NET应用程序的标识,打开文本编辑器,然后将下面的代码复制到一个新文件getId.aspx中。在浏览器中查看,浏览器中将显示 ASP.NET 应用程序的模拟标识
<%@ Page Language="C#" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>
命令行下输入c:\>aspnet_regiis -pa "NetFrameworkConfigurationKey" "ZGW\ASPNET",这个命令授予ZGW\ASPNET 帐户对计算机级别的 "NetFrameworkConfigurationKey" RSA 密钥容器的访问权限。(相反的移除命令使用-pr参数)<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>
(2) -pe选项进行加密
aspnet_regiis -pe "connectionStrings" -app "/MyApplication" 对connectionStrings节进行加密
aspnet_regiis -pd "appSettings" -app "/MyApplication"对appSettings节加密
..
(3)解密时使用-pd选项.
(4)查看已解密的配置值
打开文本编辑器,然后将下面的 ASP.NET 代码复制到一个新文件中。
1<%@ Page Language="C#" %>
2<%@ Import Namespace="System.Configuration" %>
3<%@ Import Namespace="System.Web.Configuration" %>
4<script runat="server">
5
6public void Page_Load()
7{
8 ConnectionStringsGrid.DataSource = ConfigurationManager.ConnectionStrings;
9 ConnectionStringsGrid.DataBind();
10
11 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
12 MachineKeySection key =
13 (MachineKeySection)config.GetSection("system.web/machineKey");
14 DecryptionKey.Text = key.DecryptionKey;
15 ValidationKey.Text = key.ValidationKey;
16}
17
18</script>
19<html>
20
21<body>
22
23<form runat="server">
24
25 <asp:GridView runat="server" CellPadding="4" id="ConnectionStringsGrid" />
26 <P>
27 MachineKey.DecryptionKey = <asp:Label runat="Server" id="DecryptionKey" /><BR>
28 MachineKey.ValidationKey = <asp:Label runat="Server" id="ValidationKey" />
29
30</form>
31
32</body>
33</html>
2<%@ Import Namespace="System.Configuration" %>
3<%@ Import Namespace="System.Web.Configuration" %>
4<script runat="server">
5
6public void Page_Load()
7{
8 ConnectionStringsGrid.DataSource = ConfigurationManager.ConnectionStrings;
9 ConnectionStringsGrid.DataBind();
10
11 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
12 MachineKeySection key =
13 (MachineKeySection)config.GetSection("system.web/machineKey");
14 DecryptionKey.Text = key.DecryptionKey;
15 ValidationKey.Text = key.ValidationKey;
16}
17
18</script>
19<html>
20
21<body>
22
23<form runat="server">
24
25 <asp:GridView runat="server" CellPadding="4" id="ConnectionStringsGrid" />
26 <P>
27 MachineKey.DecryptionKey = <asp:Label runat="Server" id="DecryptionKey" /><BR>
28 MachineKey.ValidationKey = <asp:Label runat="Server" id="ValidationKey" />
29
30</form>
31
32</body>
33</html>
将看到加密的 Web.config 文件中已解密的值