练习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系列文章》

转载于:https://www.cnblogs.com/Terrylee/archive/2006/10/06/Data_Access_Application_Block_HandsOnLab_Part3.html

Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)相关推荐

  1. Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)

    练习一:使用数据访问程序块执行静态SQL语句<?XML:NAMESPACE PREFIX = O /> 该练习示范了如何使用数据访问程序块进行最基本的数据访问,另外还有如何去配制程序块,提 ...

  2. Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)

    练习1:应用程序安全性<?XML:NAMESPACE PREFIX = O /> 通过该练习将在一个已经存在的应用程序中添加认证和基于角色的授权. 第一步 打BugSmak.sln项目,默 ...

  3. Enterprise Library 2.0 Hands On Lab 翻译(14):加密应用程序块(一)

    练习1:加解密字符串 通过本练习将学习通过加密来保护信息,在这里创建一个类似于IM的聊天应用程序,加密通信过程中的信息. 第一步 打BugSmak.sln项目,默认的安装路径应该为C:\Program ...

  4. Enterprise Library 5.0发布

    Microsoft Enterprise Library 5.0是一套可重用的应用程序块,帮助开发人员进行企业应用开发.包括:Caching Block.Cryptography Block.Data ...

  5. Microsoft Enterprise Library 简介与请大家下载Microsoft Enterprise Library 5.0体验微软最新技术应用于企业信息平台

    什么是Enterprise Library     Enterprise Library是一组应用程序块(Application Block)的集合.他们是可重用的软件组件,被设计用来帮助开发者面对常 ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...

  7. Enterprise Library 2.0 技巧(3):记录ASP.NET站点中未处理的异常

    这篇文章不能算是Enterprise Library 2.0的一个技巧,只是Logging Application Block的一个简单应用而已,在这里我们使用Logging Application ...

  8. Microsoft Enterprise Library 4.0 for Visual Studio 2008

    Microsoft Enterprise Library 4.0 for Visual Studio 2008已经发布,可以下载看看 转载于:https://www.cnblogs.com/rover ...

  9. Enterprise Library 3.0 体验(3):使用配置文件的Validation Application Block

    摘要:Enterprise Library 3.0 January 2007 CTP版发布了,这次发布的版对于Validation Application Block有了很大的改进,包括对配置工具的支 ...

最新文章

  1. linux环境insight安装与使用
  2. ueditor版本python3_GitHub - crazyinstall/DjangoUeditor3: Django Ueditor 兼容Python3改进,Python2上也可用...
  3. strust2 和 hibernate的整合------登录的实现
  4. 查看mysql单个表大小限制_查看单个mysql数据库中各个表的大小
  5. 03.09 随手记(Mock数据生成器,Easy Mock基本使用)
  6. solidworks电气元件3d库_送软件 | 零基础也可以学的EPLAN电气设计实战教程
  7. 搜狗新闻语料库,构建Word2Vec中文词向量
  8. 早餐为啥不能吃大米粥?医生:不仅是米粥,这3物也最好少吃
  9. Openwrt 镜像安装
  10. WPF 鼠标滚轮对图片的缩放
  11. staring mysql_mysql启动错误:Starting MySQL.. ERROR! The server quit without updating PID file错误...
  12. 让我们再聊聊TDD 续-正其思规其行
  13. sparse_categorical_crossentropy的使用
  14. 18.鸡尾酒疗法C语言
  15. 新氧科技与京东健康签订合作协议 迎来医美服务高品质上飞跃
  16. P2P下载工作原理简要解释
  17. BB方案AR眼镜Viture One解析,颈挂+磁吸有点意思
  18. 解决飞Q 在win7下 找不到的问题
  19. 机器学习算法----贝叶斯网络
  20. ios 图片加载内存尺寸_iOS加载超清大图内存暴涨问题解决

热门文章

  1. Linux创建多个子线程并回收
  2. z3 C++学习笔记
  3. Supervisor监控
  4. gdb调试(如何跟踪指定进程)
  5. 组件化与插件化的差别在哪里?内含福利
  6. 列表,元组和range
  7. Container Injection
  8. Linux iptables:规则原理和基础
  9. 第六十二节,html分组元素
  10. wordpress 基础文件