当前位置: 首页 > 图文教程 > .Net技术 > ASP.NET > ASP.NET:ASP.NET中配置文件的加密与解密

ASP.NET
浅谈如何在ASP.NET中了解LINQ语句性能
MRS实现和扩展一个Service Contract
ASP.NET:ASP.NET创建Web服务之声明XML Web服务
ASP.NET:asp.net中常用的一些小技巧
ASP.NET:ASP.NET 数据库缓存依赖
ASP.NET:.NET监控技术应用与分析
Asp.net:Asp.net多语言
ASP.NET:asp.net中接口和抽象类及区别概述
ASP.NET:小编谈理解.NET委托和事件
ASP.NET:小编浅谈asp.net(500) 内部服务器错误解决方法
ASP.NET:C#.Net程序开发中的Socket介绍
ASP.NET:小编浅述面向接口的编程
ASP.NET:.Net参数基础论
ASP.NET:小议枚举类型
ASP.NET:小编浅谈.NET多态中的重写与重载
ASP.NET:浅谈.NET垃圾回收机制(GC)
ASP.NET:ASP.NET中把彩色图片变成黑白图片
ASP.NET:ASP.NET中配置文件的加密与解密
ASP.NET:ASP.NET实现用户第二次访问网站不用提交信息
ASP.NET:ASP.NET利用Cookie处理网上重复投票

ASP.NET:ASP.NET中配置文件的加密与解密


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

ASP.NET中配置文件的加密与解密在实际开发中应用广泛,下面就具体谈谈对ASP.NET配置文件的加密与解密的用法。

 web.config可以保持数据库连接字符串,web.configXML语法结构,很容易读懂,同时非法用户也可以直接到期数据库连接信息,造成不必要的损失。下面,使用一个简单有效的加密算法来加密一段数据库连接字符,使非法用户不能清楚地得到这些信息。

 

在通常情况下,保持数据库连接字符串:

<appSettings>

<add key=”ConnectionString” value=”server=(Local);database = test; pwd=sa;uid=sa;”/>

</appSettings

 

对其进行加密的代码如下:

Configurationconfig= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection section config.GetSection(”appSettings”);

if(section !=null && !section.SectionInformation.IsProtected)

{

   Section.SectionInformation.ProtectSection(”RsaProtectedConfigurationProvider”);

   Config.Save();

}

 

对其进行解密的代码如下:

Configuration config=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection section =config.GetSection(“appSettings”);

If(section !=null && secion.SectionInformation.IsProtected)

{

  Section SectionInformation.UnprotectSection();

  Config.Save();

}