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分钟。

更多Enterprise Library的文章请参考《Enterprise Library系列文章
作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2006-10-06 12:39 TerryLee 阅读(6003) 评论(12)  编辑 收藏 网摘 所属分类: [10]  模式与实践

  回复  引用    
#1楼 2006-10-07 18:49 | hi [未注册用户]
不知道为什么,用了Enterprise Library 2.0中的Data Access Application Block后,跟踪sql语句发现,每关闭一次sql连接,就会有exec sp_reset_connection 这个语句在执行.


还请terryLee帮帮忙啊.谢谢啦!!!
  回复  引用  查看    
#2楼 [楼主]2006-10-07 21:11 | TerryLee      
@hi
这个语句是在哪儿出现的?能不能说明白一点,呵呵:-)
  回复  引用    
#3楼 2007-01-19 17:15 | deaky [未注册用户]
为什么只能这样运行
"选择Debug | Start Without Debugging菜单命令并运行应用程序"
直接运行,ConnectionStrings部份却不能加密
为什么?

  回复  引用  查看    
#4楼 [楼主]2007-01-20 00:28 | TerryLee      
@deaky
先编译为.EXE后,再运行应用程序看能不能加密?

在调试状态下是无法加密的




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-10-06 13:19 编辑过
Google站内搜索

相关文章:

相关链接: