版本:1.0 [2006-11-18]
本教程介紹 NBearV3 實體配置檔案的加密支援功能。
透過本教程,讀者應能全面掌握加密實體配置檔案,以及如何結合 ASP.NET 設定區段加密功能,實現部署環境相關的實體配置加密。
本教程不包含演示代碼。
不超過 10 分鐘。
需如下設定 entityConfig 設定區段:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="entityConfig" type="NBear.Common.EntityConfigurationSection, NBear.Common" />
</configSections>
<entityConfig encrypt="false" key="aslkjkljlsajsuaggasfklrjuisdhaie">
<includes>
<add key="Sample" value="C:\Teddy\NBearV3\src\NBear.Test.CaseTests\EntityConfig.xml" />
</includes>
</entityConfig>
<connectionStrings>
<add name="CaseTests" connectionString="Server=(local);Database=CaseTests;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
</connectionStrings>
</configuration>
關鍵屬性:
encrypt="true":啟用實體配置檔案加密。key:指定用於加密/解密實體配置檔案的金鑰。啟用加密功能後,目前應用程式將只能識別經過加密的實體配置檔案。
使用 dist 目錄下的 NBear.Tools.ConfigurationEncrypter.exe 工具(Windows Form 程式)。操作簡單:載入配置檔案、指定加密金鑰、執行加密/解密。
ASP.NET 提供配置檔加密功能。以下命令可將 IIS 中指定應用程式的 Web.config 中 entityConfig 區段加密:
aspnet_regiis -pe "entityConfig" -app "/應用程式名稱"
對應的解密命令:
aspnet_regiis -pd "entityConfig" -app "/應用程式名稱"
註 1:可執行 aspnet_regiis /help 查看更多選項。工具通常位於 [windir]\Microsoft.NET\Framework\v2.0.50727\ 目錄。
註 2:可能需將 dist\NBear.Common.dll 複製至 aspnet_regiis.exe 所在目錄,否則加密/解密過程可能報無法載入 NBear.Common.dll 錯誤。
一經 aspnet_regiis 加密,該區段內容會以加密資料取代,這些加密資料僅在執行加密的那台伺服器上能被識別。即使完整複製應用程式到另一台伺服器,也無法執行,因為配置檔無法正確識別。aspnet_regiis 尚提供更多彈性加密選項,屬 ASP.NET SDK 文件討論範疇,此處僅給出常用方法。
正文結束