提供加密 ASP.NET 應用程式之組態檔區段的逐步範例。
「受保護的組態」讓您加密儲存在 Web.config 檔中的敏感性資訊,有助於改善應用程式的安全性。您可以使用 aspnet_regiis.exe 加密 Web.config 檔的區段,以及管理加密金鑰。ASP.NET 會在處理組態檔時解密該檔案。因此,解密並不需要任何額外的程式碼。如需受保護的組態的詳細資訊,請參閱使用受保護的組態加密組態資訊。
在瀏覽這份逐步解說期間,您將瞭解如何執行下列處理:
若要完成這個逐步解說,您必須進行下列步驟:
在 ASP.NET 可以解密 Web.config 檔中的加密資訊之前,ASP.NET 應用程式的識別 (Identity) 必須具有用來加密和解密加密之區段的加密金鑰存取權限。這個逐步解說會使用預設 RsaProtectedConfigurationProvider 提供者,這個提供者是在 Machine.config 檔中指定,且命名為 "RsaProtectedConfigurationProvider"。預設 RsaProtectedConfigurationProvider 提供者所使用之 RSA 金鑰容器 (Container) 的名稱為 "NetFrameworkConfigurationKey"。
若要授與 ASP.NET 識別預設 RSA 金鑰容器的讀取權限
-
開啟文字編輯器,然後將下列程式碼複製到新檔案中。
<%@ Page Language="VB" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name)
%>
<%@ Page Language="C#" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>
-
將檔案以下列名稱儲存在應用程式目錄中:identity.aspx。
-
若要判斷 ASP.NET 應用程式的識別,請用瀏覽器開啟 identity.aspx。
ASP.NET 應用程式的模擬識別會顯示在瀏覽器中。
注意事項 |
|---|
| 因為這個逐步解說是使用 IIS,所以不要使用模擬進行網站的驗證 (Authentication)。也就是說,針對這個逐步解說,只會使用匿名驗證做為 ASP.NET 應用程式的識別。如果應用程式的識別是應用程式之 Web.config 檔中目前登入的使用者 ID (User ID) (如 DOMAIN\myuserid),請停用模擬。若要停用 Web.config 檔中的模擬,請開啟 Web.config 檔,然後移除 <identity> 項目。移除 <identity> 項目之後,請在瀏覽器中更新 identity.aspx,以顯示應用程式的已修改識別。 |
-
在命令提示字元中,使用下列選項執行 aspnet_regiis.exe:
例如,下列命令會將 NETWORK SERVICE 帳戶存取權授與電腦層級 "NetFrameworkConfigurationKey" RSA 金鑰容器。
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 檔。
-
確定具有 <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:
例如,下列命令會加密名為 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 程式碼複製到新檔案中。
<%@ 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>
-
將檔案另存為 walkthrough.aspx,然後在瀏覽器中檢視該檔案。
您將會看到已加密之 Web.config 檔的解密值。
-
若要將網站的敏感性資訊保留為私用資訊,請在完成這個逐步解說時刪除 walkthrough.aspx 檔。
如果將 aspnet_regiis.exe 與 -pd 選項搭配執行,則請在需要時解密已加密的 Web.config 檔內容。除了未指定受保護的組態提供者外,語法與使用 -pe 選項加密 Web.config 檔內容相同。使用 protected 區段的 configProtectionProvider 項目會識別適當提供者。例如,下列命令會解密 ASP.NET 應用程式 (名為 MyApplication) 之 Web.config 檔中的 <connectionStrings> 項目,以及 <system.web> 項目的 <machineKey> 子項目。
aspnet_regiis -pd "connectionStrings" -app "/MyApplication"
aspnet_regiis -pd "system.web/machineKey" -app "/MyApplication"
工作
逐步解說:建立和匯出 RSA 金鑰容器
其他資源
使用受保護的組態加密組態資訊