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

ASP.NET
ASP.NET:小编教你使用Substitution控件在缓存页面插入内容
ASP.NET:网络上实现单点登录
ASP.NET:小编浅谈泛型的使用
ASP.NET:网络验证的一些技巧杂谈
ASP.NET:小编教你实现数字和英文字母组合的验证码图片
ASP.NET:页面尺寸自动适应 1024*768和800*600分辨率
ASP.NET:如何复制数组中一系列元素的元素
ASP.NET:小编谈用Remoting技术传送文件

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 204 ::
收藏到网摘: 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();

}