如何加密ASP.NET配置資料[]

來源:互聯網
上載者:User

我已經討論過如何應用ASP.NET 2.0中的設定檔並解釋了設定檔處理,現在,我將說明如何通過加密方法保護儲存在設定檔中的資料,並描述ASP.NET 2.0中的新特性。

我首先概括敘述加密選項,然後繼續說明設定檔中資料值的實際加密過程。

受保護的配置

ASP.NET 2.0推出了一個受保護的配置(protected configuration)特性,允許您使用資料加密API(DPAPI)或RSA加密對machine.config和web.config檔案進行加密。開發人員一直希望這種類型的功能,以便可以對連接字串、賬戶認證等敏感性資料加以保護。

這項功能允許開發人員加密設定檔的一個或幾個部分。下表列出了一些可以進行加密的部分:

  • appSettings:定義和儲存定製應用值。
  • connectionStrings:通過資料庫連接字串訪問外部資料源。
  • Identity:包含Web應用程式身份,其中可能包括類比認證。
  • SessionState:給當前應用程式配置工作階段狀態設定。

您不能使用受保護配置特性來配置web.config和machine.config檔案的以下部分:

  • processMode
  • runtime
  • mscorlib
  • startup
  • system.runtime.remoting
  • configProtectedData
  • satelliteassemblies
  • cryptographySettings
  • cryptoNameMapping
  • cryptoClasses

.NET Framework提供兩種方法加密設定檔:aspnet_regiis.exe命令列工具 + 生產力和開發人員應用程式代碼加密。本文主要說明命令列加密方法。

ASP.NET IIS註冊工具

ASP.NET IIS註冊工具(aspnet_regiis.exe)是.NET Framework架構的一個標準組成部分。它允許您更新一個ASP.NET應用程式的指令碼映射,使其指向與工具相關的ASP.NET ISAPI版本,因為一個系統中可能存在幾個ASP.NET版本。您還可以用這個工具來顯示所有已安裝ASP.NET版本的狀況,註冊與工具關聯的ASP.NET版本,建立客戶-指令碼目錄,並執行其它配置操作。

這個工具包含大量的命令列選項,包括加密(pef)和解密(pdf)。您可以使用/?選項獲得協助。加密選項利用DPAPI加密資料。下面我將以列表A中的web.config檔案為例進行說明:

列表A

<?xml version="1.0"?>

<configuration>

<appSettings>

<add key="site" value="TechRepublic.com" />

</appSettings>

<connectionStrings>

<add name="db" connectionString="connection details" />

</connectionStrings>

<system.web>

<compilation debug="true"/>

<authentication mode="Windows"/>

<authorization>

<allow users="tester"/>

</authorization>

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<error statusCode="403" redirect="NoAccess.htm"/>

<error statusCode="404" redirect="FileNotFound.htm"/>

</customErrors>

</system.web>

</configuration>

有了這個檔案,我就可以用下面的命令列加密設定檔connectionStrings部分的內容:

aspnet_regiis -pef "connectionStrings" "c:inetpubwwwroottrconfig"

上面命令的結構非常簡單,如下表所述:

  • aspnet_regiis:ASP.NET IIS註冊工具。
  • -ped:加密一個設定檔配置部分的命令列選項。
  • “connectionStrings”:被加密部分的名稱。
  • “c:inetpubwwwroottrconfig”:以磁碟為基礎的網站的物理地址。

完成後,您將收到一段成功或失敗資訊。例如,列表B中包含connectionStrings部分被加密的web.config檔案。(如果您希望複製並粘貼代碼,這裡是列表B的連結。)

<?xml version=”1.0″?>

<configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0″>

<appSettings>

<add key=”site” value=”TechRepublic.com” />

</appSettings>

<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>hlcvZ+8tPctwMZ5L68zsjWR5uySk5iqbGN6vNa94xrvdAEg2hIo1cexVOITMnVHdBfU7rXWoo3X5KfB3JhrBQQZdFXtAGqR7J3I4GUm6JDu7wWgM5Npb0Vyh+l6FwwEQYj7GfulTO+I3rWLkG7E44Sqzv75VG9QIU7oBH0d+jXo=</CipherValue>

</CipherData>

</EncryptedKey>

</KeyInfo>

<CipherData>

<CipherValue>oEe1fu5aiY0AtsgovXG7TVdxSZw8FU1w18LhdSmL5pptKtOnSYIZ6gVzm9B5/n4t5PWsn7BqGmd535JPe4G+TAqIyiniYQEKLXW3YVHRJX19vlwIIn6y3nyyy8gHE2kHC3LBvkqAtE7sbRlVxGSxpA==</CipherValue>

</CipherData>

</EncryptedData>

</connectionStrings>

<system.web>

<compilation debug=”true” defaultLanguage=”c#” />

<authentication mode=”None” />

<authorization>

<allow users=”tester”/>

</authorization>

<customErrors mode=”RemoteOnly” defaultRedirect=”GenericErrorPage.htm”>

<error statusCode=”403″ redirect=”NoAccess.htm”/>

<error statusCode=”404″ redirect=”FileNotFound.htm”/>

</customErrors>

</system.web>

</configuration>

檔案的其它部分沒有變化,但您也可以選擇加密其它部分。例如,您可以加密執行個體檔案中的appSettings部分,屏蔽它的資料,以免別人窺視。

如果您使用一個加密檔案,您就需要對它進行解密,用命令列可以方便地進行解密。下面的命令列將前面web.config檔案的connectionStrings部分應用的加密過程進行解密。

aspnet_regiis -pdf "connectionStrings" "c:inetpubwwwroottrconfig"

這個命令將加密檔案返回到明文版本,因此您可以對其進行修改,並在編輯完成後重新加密。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.