http://blog.hunanw.com/u/kary/archives/2006/166.html

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) |
更多 |

产品家族 | 产品资源关于微软 | 热门栏目 |




MSDN Library
开发工具和语言
Visual Studio 2008
Visual Studio 文档
安装和设置要点
Visual Studio 简介
Visual Studio 中的应用程序开发
基于 Windows 的应用程序、组件和服务
Visual Studio 中的 .NET Framework 编程
Visual Basic
Visual C#
Visual C++
Visual J#
JScript
Visual Web Developer
ASP.NET 和 Web 开发新增功能
ASP.NET 概述
ASP.NET 演练精选
使用 Visual Web Developer
创建 ASP.NET 网页
创建 ASP.NET 网站
理解 ASP.NET 基础结构
使用 ASP.NET 控件
创建自定义 ASP.NET 控件路线图
添加客户端功能和 AJAX
通过 ASP.NET 访问数据
使用 ASP.NET Web 服务
保证 ASP.NET 网站的安全
管理 ASP.NET 网站
生成和部署 ASP.NET 网站
性能、故障排除和调试
迁移和转换 ASP.NET 网站
针对移动设备开发网站
Visual Web Developer 设置
Visual Web Developer 用户界面元素
Visual Studio Tools for Office
智能设备开发
工具和功能
Visual Studio SDK
Visual Studio 和 .NET Framework 词汇表
Visual Studio Team System 文档
Video How Tos
Technical Articles
Visual Studio 2005
.NET Development
SQL Server
技术文章
提供有关加密 ASP.NET 应用程序配置文件的节的逐步骤示例。
由于能对存储在 Web.config 文件中的敏感信息进行加密,“受保护的配置”有助于提高应用程序的安全性。可以使用 aspnet_regiis.exe 对 Web.config 文件的节进行加密并管理加密密钥。ASP.NET 在处理文件时对配置文件进行解密。因此,解密不需要任何附加代码。有关受保护的配置的更多信息,请参见使用受保护的配置加密配置信息。
在本演练中,您将学会如何执行以下任务:
-
使用默认“受保护配置”提供程序对 Web.config 文件的节进行加密。
-
访问 ASP.NET 页中已解密的配置信息。

若要完成本演练,您需要:
-
在将要承载该站点的计算机上安装并配置 Microsoft Internet 信息服务 (IIS)。
有关如何安装和配置 IIS 的详细信息,请参见 IIS 安装随附的联机帮助或转到 “Internet Information Services (IIS) 6.0 Technical Resources”(Internet 信息服务 (IIS) 6.0 技术资源)。
-
ASP.NET 网站。
如果您有网站,则可以使用该网站。否则,有关如何创建虚拟目录或网站的详细信息,请参见如何:在 IIS 5.0 和 6.0 中创建和配置虚拟目录。

ASP.NET 应用程序的标识必须能读取用于加密和解密已加密节的加密密钥,才能对 Web.config 文件中的已加密信息进行解密。此演练使用 Machine.config 文件中指定的名为 "RsaProtectedConfigurationProvider" 的默认 RsaProtectedConfigurationProvider 提供程序。默认 RsaProtectedConfigurationProvider 提供程序使用的 RSA 密钥容器名为 "NetFrameworkConfigurationKey"。
授予 ASP.NET 标识对默认 RSA 密钥容器的读取权限
-
打开文本编辑器,然后将下面的代码复制到一个新文件中。
Visual Basic<%@ Page Language="VB" %> <% Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name) %>
C#<%@ Page Language="C#" %> <% Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name); %>
-
在应用程序目录中将该文件另存为 “identity.aspx”。
-
若要确定 ASP.NET 应用程序的标识,请在浏览器中打开 identity.aspx。
浏览器中将显示 ASP.NET 应用程序的模拟标识。
说明:
因为将在本演练中使用 IIS,所以不要对您的站点身份验证使用模拟。即只对该演练使用匿名身份验证作为 ASP.NET 应用程序的标识。如果应用程序的标识为目前登录的用户 ID(如 DOMAIN\myuserid),请在应用程序的 Web.config 文件中禁用模拟。若要在 Web.config 文件中禁用模拟,请打开 Web.config 文件,然后移除 <identity> 元素。移除 <identity> 元素后,请更新浏览器中的 identity.aspx 以显示修改后的应用程序标识。
-
在命令提示处,运行带有下列选项的 aspnet_regiis.exe:
-
-pa 选项,后面跟默认 RsaProtectedConfigurationProvider 的 RSA 密钥容器的名称。
-
在前一步中确定的 ASP.NET 应用程序的标识。
例如,下面的命令授予 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> 节
-
在文本编辑器中,打开应用程序的 Web.config 文件。
-
如果没有 ASP.NET 应用程序的 Web.config 文件,请打开文本编辑器并将示例配置复制到一个新文件中,然后在 ASP.NET 应用程序目录中将该文件另存为 “web.config”。
-
-
如下面的实例所示,确保 <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>
-
关闭 Web.config 文件。
-
在命令提示处,通过键入以下命令将目录更改为 .NET Framework 2.0 版目录:
cd \WINDOWS\Microsoft.Net\Framework\v2.0.*
-
在命令提示处,运行带有下列选项的 aspnet_regiis.exe:
-
-pe 选项和字符串 "connectionStrings" 用于对应用程序的 Web.config 文件的 connectionStrings 元素进行加密。
-
-app 选项和应用程序的名称。
例如,下面的命令将加密 MyApplication 应用程序的 Web.config 文件的 <connectionStrings> 节。
aspnet_regiis -pe "connectionStrings" -app "/MyApplication"
-
-
对于 <system.web> 元素的 <machineKey> 子元素重复上述步骤,如下面的示例所示:
aspnet_regiis -pe "system.web/machineKey" -app "/MyApplication"
请勿关闭“命令提示”窗口。
-
打开 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>
-
关闭 Web.config 文件。

ASP.NET 在处理 Web.config 文件时会自动对该文件的内容进行解密。因此,不需要任何附加步骤即可对已加密的配置设置进行解密,供其他 ASP.NET 功能使用或用于访问代码中的值。 但是,若要查看已解密的设置,您可以遵循下面的步骤操作。
查看已解密的配置值
-
打开文本编辑器,然后将下面的 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>
C#<%@ 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>
-
将该文件另存为 “walkthrough.aspx”,然后在浏览器中查看该文件。
您将看到加密的 Web.config 文件中已解密的值。
-
若要保持网站上敏感信息的私密性,请在完成此演练时删除 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"



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

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