演练:使用受保护的配置加密配置信息

本文介绍如何使用受保护的配置加密ASP.NET应用程序中的敏感信息。通过示例演示如何加密Web.config文件中的连接字符串和machineKey节,并展示如何在ASP.NET页面中访问这些已解密的配置信息。
http://msdn2.microsoft.com/zh-cn/library/dtkwfdky.aspx
http://blog.hunanw.com/u/kary/archives/2006/166.html
欢迎您  |  登录
请单击此处以下载 Silverlight。   | 
中国 - 简体中文  Dropdown Arrow
Argentina (Español)
Australia (English)
Brasil (Português)
Canada (English)
Canada (Français)
中国 (简体中文)
Colombia (Español)
Deutschland (Deutsch)
España (Español)
France (Français)
India (English)
Italia (Italiano)
México (Español)
Perú (Español)
Россия (Pусский)
United Kingdom (English)
United States (English)
更多
 | 
请单击以进行评分并提供反馈
请就此内容提供反馈
 演练:使用受保护的配置加密配置信息
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET
演练:使用受保护的配置加密配置信息

提供有关加密 ASP.NET 应用程序配置文件的节的逐步骤示例。

由于能对存储在 Web.config 文件中的敏感信息进行加密,“受保护的配置”有助于提高应用程序的安全性。可以使用 aspnet_regiis.exe 对 Web.config 文件的节进行加密并管理加密密钥。ASP.NET 在处理文件时对配置文件进行解密。因此,解密不需要任何附加代码。有关受保护的配置的更多信息,请参见使用受保护的配置加密配置信息

在本演练中,您将学会如何执行以下任务:

  • 使用默认“受保护配置”提供程序对 Web.config 文件的节进行加密。

  • 访问 ASP.NET 页中已解密的配置信息。

若要完成本演练,您需要:

ASP.NET 应用程序的标识必须能读取用于加密和解密已加密节的加密密钥,才能对 Web.config 文件中的已加密信息进行解密。此演练使用 Machine.config 文件中指定的名为 "RsaProtectedConfigurationProvider" 的默认 RsaProtectedConfigurationProvider 提供程序。默认 RsaProtectedConfigurationProvider 提供程序使用的 RSA 密钥容器名为 "NetFrameworkConfigurationKey"

授予 ASP.NET 标识对默认 RSA 密钥容器的读取权限

  1. 打开文本编辑器,然后将下面的代码复制到一个新文件中。

    Visual Basic
    <%@ Page Language="VB" %>
                <%
                Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name)
                %>
                

     

    <%@ Page Language="C#" %>
                <%
                Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                %>
                
  2. 在应用程序目录中将该文件另存为 “identity.aspx”

  3. 若要确定 ASP.NET 应用程序的标识,请在浏览器中打开 identity.aspx。

    浏览器中将显示 ASP.NET 应用程序的模拟标识。

    说明:

    因为将在本演练中使用 IIS,所以不要对您的站点身份验证使用模拟。即只对该演练使用匿名身份验证作为 ASP.NET 应用程序的标识。如果应用程序的标识为目前登录的用户 ID(如 DOMAIN\myuserid),请在应用程序的 Web.config 文件中禁用模拟。若要在 Web.config 文件中禁用模拟,请打开 Web.config 文件,然后移除 <identity> 元素。移除 <identity> 元素后,请更新浏览器中的 identity.aspx 以显示修改后的应用程序标识。

  4. 在命令提示处,运行带有下列选项的 aspnet_regiis.exe:

    例如,下面的命令授予 NETWORK SERVICE 帐户对计算机级别的 "NetFrameworkConfigurationKey" RSA 密钥容器的访问权限。

    说明:

    在运行 Microsoft Windows Server 2003 的计算机上,如果在 Web.config 文件中禁用了 ASP.NET 应用程序的模拟,则该应用程序的标识将为应用程序池的标识。默认情况下,为 NETWORK SERVICE 帐户(在 Windows 的早期版本中,该标识为本地 ASPNET 帐户)。

    aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"

    请勿关闭“命令提示”窗口。

既然 ASP.NET 应用程序的标识能够读取默认 RsaProtectedConfigurationProvider 对象的 RSA 密钥容器,就可以使用该密钥容器对 ASP.NET 应用程序的 Web.config 文件的节进行加密。然后,ASP.NET 在处理 Web.config 文件时会对这些节进行解密。

加密 Web.config 文件的 <connectionStrings> 和 <machineKey> 节

  1. 在文本编辑器中,打开应用程序的 Web.config 文件。

    • 如果没有 ASP.NET 应用程序的 Web.config 文件,请打开文本编辑器并将示例配置复制到一个新文件中,然后在 ASP.NET 应用程序目录中将该文件另存为 “web.config”

  2. 如下面的实例所示,确保 <system.web><connectionStrings> 子元素和 <machineKey> 子元素。

    <configuration>
                <connectionStrings>
                <add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" />
                </connectionStrings>
                <system.web>
                <machineKey validationKey="D61B3C89CB33A2F1422FF158AFF7320E8DB8CB5CDA1742572A487D94018787EF42682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE"
                decryptionKey="FBF50941F22D6A3B229EA593F24C41203DA6837F1122EF17" />
                </system.web>
                </configuration>
  3. 关闭 Web.config 文件。

  4. 在命令提示处,通过键入以下命令将目录更改为 .NET Framework 2.0 版目录:

    cd \WINDOWS\Microsoft.Net\Framework\v2.0.*

  5. 在命令提示处,运行带有下列选项的 aspnet_regiis.exe:

    • -pe 选项和字符串 "connectionStrings" 用于对应用程序的 Web.config 文件的 connectionStrings 元素进行加密。

    • -app 选项和应用程序的名称。

    例如,下面的命令将加密 MyApplication 应用程序的 Web.config 文件的 <connectionStrings> 节。

    aspnet_regiis -pe "connectionStrings" -app "/MyApplication"

  6. 对于 <system.web> 元素的 <machineKey> 子元素重复上述步骤,如下面的示例所示:

    aspnet_regiis -pe "system.web/machineKey" -app "/MyApplication"

    请勿关闭“命令提示”窗口。

  7. 打开 Web.config 文件并查看已加密的内容。

    内容看上去类似于下面的示例 Web.config 文件。

    <configuration>
                <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
                <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
                xmlns="http://www.w3.org/2001/04/xmlenc#">
                <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                <KeyName>RSA Key
                </KeyName>
                </KeyInfo>
                <CipherData>
                <CipherValue>WcFEbDX8VyLfAsVK8g6hZVAG1674ZFc1kWH0BoazgOwdBfinhcAmQmnIn0oHtZ5tO2EXGl+dyh10giEmO9NemH4YZk+iMIln+ItcEay9CGWMXSen9UQLpcQHQqMJErZiPK4qPZaRWwqckLqriCl9X8x9OE7jKIsO2Ibapwj+1Jo=
                </CipherValue>
                </CipherData>
                </EncryptedKey>
                </KeyInfo>
                <CipherData>
                <CipherValue>OpWQgQbq2wBZEGYAeV8WF82yz6q5WNFIj3rcuQ8gT0MP97aO9SHIZWwNggSEi2Ywi4oMaHX9p0NaJXG76aoMR9L/WasAxEwzQz3fexFgFSrGPful/5txSPTAGcqUb1PEBVlB9CA71UXIGVCPTiwF7zYDu8sSHhWa0fNXqVHHdLQYy1DfhXS3cO61vW5e/KYmKOGA4mjqT0VZaXgb9tVeGBDhjPh5ZlrLMNfYSozeJ+m2Lsm7hnF6VvFm3fFMXa6+h0JTHeCXBdmzg/vQb0u3oejSGzB4ly+V9O0T4Yxkwn9KVDW58PHOeRT2//3iZfJfWV2NZ4e6vj4Byjf81o3JVNgRjmm9hr9blVbbT3Q8/j5zJ+TElCn6zPHvnuB70iG2KPJXqAj2GBzBk6cHq+WNebOQNWIb7dTPumuZK0yW1XDZ5gkfBuqgn8hmosTE7mCvieP9rgATf6qgLgdA6zYyVV6WDjo1qbCV807lczxa3bF5KzKaVUSq5FS1SpdZKAE6/kkr0Ps++CE=
                </CipherValue>
                </CipherData>
                </EncryptedData>
                </connectionStrings>
                <system.web>
                <machineKey configProtectionProvider="RsaProtectedConfigurationProvider">
                <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
                xmlns="http://www.w3.org/2001/04/xmlenc#">
                <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                <KeyName>RSA Key
                </KeyName>
                </KeyInfo>
                <CipherData>
                <CipherValue>IwUopItbWX0mJdGWtAqE1LlsG3u5RBRlAXs9/GZj3HEfeUXduHVF76q6Ip88YqlfLthH+DMBYdOZAF+hCOmS2agfTo1tKUvELRGIljS/BqEYxUO+/IOz9tllAw8ZlGF7AVCzptgIejI+iLXEZfMKW7f6EMGeb5vaaKXHIkYZwcM=
                </CipherValue>
                </CipherData>
                </EncryptedKey>
                </KeyInfo>
                <CipherData>
                <CipherValue>ivVyERVPNUzIb/i7/NUbRkxsxh8IG959vycwrzJO0vYWxHZ5i03SfrLbsGUV17+FxZ6lbcrVaF5FY3zVm7dRMRvQpVFwaVcL
                </CipherValue>
                </CipherData>
                </EncryptedData>
                </machineKey>
                </system.web>
                </configuration>
  8. 关闭 Web.config 文件。

ASP.NET 在处理 Web.config 文件时会自动对该文件的内容进行解密。因此,不需要任何附加步骤即可对已加密的配置设置进行解密,供其他 ASP.NET 功能使用或用于访问代码中的值。 但是,若要查看已解密的设置,您可以遵循下面的步骤操作。

查看已解密的配置值

  1. 打开文本编辑器,然后将下面的 ASP.NET 代码复制到一个新文件中。

    Visual Basic
    <%@ Page Language="VB" %>
                <%@ Import Namespace="System.Configuration" %>
                <%@ Import Namespace="System.Web.Configuration" %>
                <script runat="server">
                Public Sub Page_Load()
                ConnectionStringsGrid.DataSource = ConfigurationManager.ConnectionStrings
                ConnectionStringsGrid.DataBind()
                Dim config As System.Configuration.Configuration = _
                WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
                Dim key As MachineKeySection = _
                CType(config.GetSection("system.web/machineKey"), MachineKeySection)
                DecryptionKey.Text = key.DecryptionKey
                ValidationKey.Text = key.ValidationKey
                End Sub
                </script>
                <html>
                <body>
                <form runat="server">
                <asp:GridView runat="server" CellPadding="4" id="ConnectionStringsGrid" />
                <P>
                MachineKey.DecryptionKey = <asp:Label runat="Server" id="DecryptionKey" /><BR>
                MachineKey.ValidationKey = <asp:Label runat="Server" id="ValidationKey" />
                </form>
                </body>
                </html>
                

     

    <%@ Page Language="C#" %>
                <%@ Import Namespace="System.Configuration" %>
                <%@ Import Namespace="System.Web.Configuration" %>
                <script runat="server">
                public void Page_Load()
                {
                ConnectionStringsGrid.DataSource = ConfigurationManager.ConnectionStrings;
                ConnectionStringsGrid.DataBind();
                Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
                MachineKeySection key =
                (MachineKeySection)config.GetSection("system.web/machineKey");
                DecryptionKey.Text = key.DecryptionKey;
                ValidationKey.Text = key.ValidationKey;
                }
                </script>
                <html>
                <body>
                <form runat="server">
                <asp:GridView runat="server" CellPadding="4" id="ConnectionStringsGrid" />
                <P>
                MachineKey.DecryptionKey = <asp:Label runat="Server" id="DecryptionKey" /><BR>
                MachineKey.ValidationKey = <asp:Label runat="Server" id="ValidationKey" />
                </form>
                </body>
                </html>
                
  2. 将该文件另存为 “walkthrough.aspx”,然后在浏览器中查看该文件。

    您将看到加密的 Web.config 文件中已解密的值。

  3. 若要保持网站上敏感信息的私密性,请在完成此演练时删除 walkthrough.aspx 文件。

可以根据需要通过运行带有 -pd 选项的 aspnet_regiis.exe 对已加密的 Web.config 文件内容进行解密。语法与使用 -pe 选项对 Web.config 文件内容进行加密的语法相同,区别在于无需指定“受保护配置”提供程序。相应的提供程序使用 protected 节的 configProtectionProvider 元素标识。例如,下面的命令在名为 MyApplication 的 ASP.NET 应用程序的 Web.config 文件中对 <connectionStrings> 元素和 <system.web> 元素的 <machineKey> 子元素进行解密。

aspnet_regiis -pd "connectionStrings" -app "/MyApplication"

aspnet_regiis -pd "system.web/machineKey" -app "/MyApplication"

标记 What's this?: 添加标记 添加   取消
社区内容
 
添加 社区内容
您贡献的内容可能需要几分钟才会显示
标记:
© 2008 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker
DCSIMG
安装 Microsoft Silverlight,享受本站点带来的更佳体验。

体验 Silverlight 已启用的快速入门教程、视频培训、演示以及更多内容。

单击“单击进行安装”即表示您接受 Silverlight 许可协议 .
Silverlight 将自动更新,
进一步了解 .

转载于:https://www.cnblogs.com/zhaoxiaoyang/archive/2008/03/27/1125867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值