首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

Enterprise Library 2.0 Hands On Lab 翻譯(3):資料存取應用程式區塊(三)

     編輯:關於ASP.NET

練習 3:加密資料庫連線資訊

學習如何加密資料庫連線字串。

步驟 1:開啟專案

開啟 DataEx3.sln,預設路徑 C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex03\begin,編譯。

步驟 2:加密資料庫連線字串

  1. Enterprise Library 1.1 加密連線字串需依賴 Cryptography 區塊。.NET Framework 2.0 內建此功能,透過 Configuration 命名空間類別完成,支援兩種加密:
  • DPAPIProtectedConfigurationProvider:使用 Windows DPAPI
  • RsaProtectedConfigurationProvider:使用 RSA 演算法
  1. 專案 ProductMaintenance 加入參考:System.Configuration.dll
  2. 加入 System.Configuration 參考

  3. 開啟 Program.cs,加入命名空間:using System.Configuration;
  4. 在 ProtectConfiguration 方法加入代碼:
static void ProtectConfiguration()
{
    string provider = "RsaProtectedConfigurationProvider";
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection section = config.ConnectionStrings;

    if ((section.SectionInformation.IsProtected == false) &&
        (section.ElementInformation.IsLocked == false))
    {
        section.SectionInformation.ProtectSection(provider);
        section.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Full);
    }
}

步驟 3:執行驗證

執行應用程式,開啟 bin\Debug\ProductMaintenance.exe.config,連線資訊已加密:

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  <EncryptedData Type="https://www.w3.org/2001/04/xmlenc#Element" ...>
    <CipherValue>...加密內容...</CipherValue>
  </EncryptedData>
</connectionStrings>

建議三個練習完成時間共 30 分鐘。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved