当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET2.0中配置文件的加密与解密

ASP.NET
利用ASP.NET和AJAX解决手工拼接HTML问题
Asp.net关于动态输出服务器控件的应用
技巧/诀窍:在ASP.NET中重写URL
ASP.NET 自定义控件从入门到精通3
以Post方式向网页发送数据
ASP.NET实现数据采集
使用ASP.NET Image Generation生成图片缩略图及水印
ASP.NET安全问题--ASP.NET安全架构
反思软件系统与软件系统之间的集成交互问题
.Net实现程序的插件机制
作为ASP.NET开发人员必须养成的编程习惯
总结了一下ADO.NET数据库连接的相关知识
VB.NET中有用的通用对象列表
HTTP Error 503与.NET 3.5 SP1 X64
ASP.NET创建Web服务之使用事务
ASP.NET中基类Page_Load方法后执行原因分析
ASP.NET中让网页弹出窗口不再困难
改变.net网站的默认解决方案位置
.net垃圾回收和CLR 4.0对垃圾回收所做的改进之二
.net垃圾回收和CLR 4.0对垃圾回收所做的改进之一

ASP.NET2.0中配置文件的加密与解密


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 55 ::
收藏到网摘: n/a

首先添加引用: using System.Web.Configuration;

加密操作如下:

以下为引用的内容:

private void ProtectSection(string sectionName, string provider)
    {
        Configuration config =
            WebConfigurationManager.
                OpenWebConfiguration(Request.ApplicationPath);

        ConfigurationSection section = config.GetSection(sectionName);

        if (section != null && !section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection(provider);
            config.Save();
        }
    }

解密操作如下:

以下为引用的内容:


 private void UnProtectSection(string sectionName)
    {
        Configuration config =
            WebConfigurationManager.
                OpenWebConfiguration(Request.ApplicationPath);

        ConfigurationSection section = config.GetSection(sectionName);

        if (section != null && section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            config.Save();
        }
    }


实践:

加密前的配置文件:

以下为引用的内容:
<?xml version="1.0"?>
<configuration>
    <appSettings>
  <add key="name" value="shy520" />
  <add key="address" value="cnblogs" />
 </appSettings>
    <system.web>
        <compilation debug="true"/>
  </system.web>
</configuration>
加密后的配置文件:
<?xml version="1.0"?>
<configuration>
    <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
  <EncryptedData>
   <CipherData>
    <CipherValue>
      AQAAANCMnd8BFdERjHoAwE/Cl+s
      BAAAABi1ATlNkEUGEf0XyWGL2Xg
      QAAAACAAAAAAADZgAAqAAAABAAA
      ABIhxMWlazAntwIIpST1CDXAAAA
      AASAAACgAAAAEAAAAPz/YKYx07c
      b+h4fqdr4fkLgAAAAX1Ieyc+WSx
      AfsDW1vn2C/fXsG2TAnYeUgaCov
      8e3nGFTHHsH91gLiqKregFPYzGR
      vW1xrez/3VwOmJI9eS7EFKrcXej
      NnHL66kg2iNRk3ntLNZlZtTs3cZ
      9w63u47VKAjs6miWsGBz2GntL/9
      UGHLELigrJcr3YJ+lsjOscExQnv
      HGvA48EfxpD+tEiFBtgXeHsFkQX
      cqGySshx16vCt2GUNUC3ZmEAhBh
      UsAFkPIYqelYHd4+m9a/xPe2tqw
      GIbla1wbW2NDEfrzJPwnkfmpNqR
      hXijKImipwXbDVYy6o0UAAAAs8D
      suYNOhJ7qAjJa2c/4eUC7sks=
      </CipherValue>
   </CipherData>
  </EncryptedData>
 </appSettings>
    <system.web>
        <compilation debug="true"/>
  </system.web>
</configuration>