Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
練習3:加密數據庫連接信息
通過該練習,你將學會如何去加密數據庫連接信息。
?
第一步
打開DataEx3.sln項目,默認的安裝路徑應該為C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex03\begin,并編譯。
?
第二步 加密數據庫連接字符串
1.在Enterprise Library1.1中加密連接字符串,需要依賴于Cryptography Application Block。.NET Framework2.0中已經內置了這項功能,通過Configuration命名空間下的一些類來完成,支持兩種類型的加密:
DPAPIProtectedConfigurationProvider:使用Windows Data Protection API (DPAPI)
RsaProtectedConfigurationProvider:使用RSA算法
2.選擇ProductMaintenance項目,選擇Project | Add Reference …菜單命令,在彈出的對話框中選擇.NET頁并添加如下程序集。
System.Configuration.dll
3.在解決方案管理器中選擇Program.cs文件,選擇View | Code菜單命令,加入如下命名空間。
using?System.Configuration;4.在方法ProtectConfiguration中添加如下代碼。
static?void?ProtectConfiguration(){
????//?TODO:?Protect?the?Connection?Strings
????string?provider?=?"RsaProtectedConfigurationProvider";
?
????Configuration?config?=?null;
????config?=?ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
?
????ConfigurationSection?section?=?config.ConnectionStrings;
?
????if?((section.SectionInformation.IsProtected?==?false)?&&
????????(section.ElementInformation.IsLocked?==?false))
????{
????????//?Protect?(encrypt)?the?"connectionStrings"?section.
????????section.SectionInformation.ProtectSection(provider);
?
????????//?Save?the?encrypted?section.
????????section.SectionInformation.ForceSave?=?true;
????????config.Save(ConfigurationSaveMode.Full);
????}
}
?
第三步 運行應用程序
選擇Debug | Start Without Debugging菜單命令并運行應用程序,注意該示例和練習2中的示例是一樣的。在項目bin\Debug目錄中打開ProductMaintenance.exe.config配置文件,注意到連接信息已經變成了密文。
<?xml?version="1.0"?encoding="utf-8"?><configuration>
??<configSections>
????<section?name="dataConfiguration"?type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,?Microsoft.Practices.EnterpriseLibrary.Data,?Version=2.0.0.0,?Culture=neutral,?PublicKeyToken=null"?/>
??</configSections>
??<dataConfiguration?defaultDatabase="QuickStarts?Instance"?/>
??<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>xeuEp2HB0xd87DFM0p5UwO78QjRW6A/pb6kGJpS5Rl0F0jHAEPh8wz4Jroc1+/I7nvmsCo6a8wzju4Nyd5ZGF6KRZgx56P9wRgkUFtJPgDROrz1ASRSIrOfMjw4+1uedT+pl+IuF1EWgEH9Vb+/8A9xmbYWtMBAcR/f/quSC1nQ=</CipherValue>
??????????</CipherData>
????????</EncryptedKey>
??????</KeyInfo>
??????<CipherData>
????????<CipherValue>DrwCnj8uCmkWOjLc2waTGX2pf8QKRFpegQbFv0zcVAwcCkZRvUVnIj9kXCLiIx+Pcbrz6H/fccbWxybAA+V7A4unJvDXegyZR1+dW7UqfDOAagTW67FC6iI3vatOpGCw30W+xpwhfgptCoFRNiCMWqxvpv++pywSK5SNfB7UZwpl90Q9dBHmmCIVyi/ZbS5JY2FLN68nRd9CHZmZLHv9opBm4DvMVdAXt7oKQ6tk9k4HJZzpUc1V8pWLQn7NQroA/4WpUDGGgk1gJ2HTBkP2L6wATzxTfQDgZbW/JIgrdollAQbO3/UEAvAnc0swoL/6BhWS5MW/9PxjuQK6GhsnSr4Dg7SEdsFPO2bTsAP/lAUeY5y9M3UxC1Q32IwMt8O4gz5ppNgYY7R8yKmvH7/S80/i61qJXvSJEQ/hQjx8V2R9okuBaN4XVgLUysmFWsOwxxHiGFyuSOECDWnr1c/5XwM7O85gVTzMELdM+N1jVFQTADXQmckOY1nZllRd3cA9CB1Qruqn/RxbGOFHT1F6y/4Cbfk7x1CKsmHx0iI0WNJ5iD3KYEq5kosGwWxrOI8C28BiXfEztwCzruSP6JpMbw==</CipherValue>
??????</CipherData>
????</EncryptedData>
??</connectionStrings>
</configuration>
注意根據Hands On Lab給出的時間建議,做完以上三個練習的時間應該為30分鐘。
轉載于:https://www.cnblogs.com/Terrylee/archive/2006/10/06/Data_Access_Application_Block_HandsOnLab_Part3.html
總結
以上是生活随笔為你收集整理的Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到下大雨桥塌了是怎么回事
- 下一篇: 梦到试鞋是什么征兆