这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串,之前只写了第二种方式,现在将两种方式都写出来,供大家参考。不管使用哪种方式我们在程序中都不需要写任何代码来解密连接字符串,因为.NET会自动的为我们解密。如果我们要用连接字符串,可以像平常那样调用。

例如:string strconnection = ConfigurationManager.AppSettings["connection"].ToString();

第一种方式:

通过在命令行中工具运行aspnet_regiis.exe -pef命令,可以对web.config中的连接串进行加密和解密。

1、在命令窗口中,输入命令 aspnet_regiis.exe -pef "connectionStrings" "C:\VisualStudio2008\Authorization"

注:-pef表明程序是以文件系统的形式建立的。第二个“connectionStrings”是你要加密的configuration 节点名字。    第三个参数指名 web.config的物理路径。

2、成功执行命令后会显示:加密成功如果我们想解密,只需要在命令窗口中,输入aspnet_regiis.exe -pdf "connectionStrings" "C:\VisualStudio2008\Authorization"成功执行后,会显示解密成功。此时就可以对连接字符串进行修改。

第二种方式:

使用RSAProtectedConfigurationProvider和DataProtectionConfgurationProvider来加密解密web.config

下面的加密代码使用的是“DataProtectionConfgurationProvider”进行加密的,如需使用“RSAProtectedConfigurationProvider”进行加密,只需要将代码中的“DataProtectionConfgurationProvider”替换成“RSAProtectedConfigurationProvider”即可。

    /// <summary>
/// 加密指定区块
/// </summary>
private void EncryptWebConfigByDPAPI()
{
Configuration configuration = null;
ConfigurationSection connectionSection = null; //打开Request所在路径网站的Web.config文件
configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//取得Web.config中connectionStrings设置区块
connectionSection = configuration.GetSection("connectionStrings");
//未加密时
if (!connectionSection.SectionInformation.IsProtected)
{
connectionSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
configuration.Save();
}
} /// <summary>
/// 解密指定区块
/// </summary>
private void EncryptWebConfig()
{
Configuration configuration = null;
ConfigurationSection connectionSection = null;
//打开Request所在路径网站的Web.config文件
configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//取得Web.config中connectionStrings设置区块
connectionSection = configuration.GetSection("connectionStrings"); if (connectionSection != null && connectionSection.SectionInformation.IsProtected)
{
connectionSection.SectionInformation.UnprotectSection();
configuration.Save();
}
}

.net加密web.config文件的更多相关文章

  1. DataProtectionConfigurationProvider加密web.config文件

    web.config 文件中经常会包含一些敏感信息,最常见的就是数据库连接字符串了,为了防止该信息泄漏,最好是将相关内容加密. Aspnet_regiis.exe命令已经提供了加密配置文件的方法,系统 ...

  2. RSAProtectedConfigurationProvider加密web.config

    上一篇文章介绍了用 DataProtectionConfigurationProvider加密web.config文件的方法,不过他有一个缺陷,加密的文件只有在本机才能解密,如果有多台服务器的话,则需 ...

  3. 加密web.config

    当我们要进行数据库的连接时,就会根据<%$ connectionsStrings:MyConnectionStringName %>这个表达式在Web.config文件中找到和MyConn ...

  4. web.config文件中配置数据库连接的两种方式

    web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报    分类: 数据库(74)  在网站开发 ...

  5. C#中web.config文件详解

    C#中web.config文件详解 一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NE ...

  6. Web.config文件 详解

    一.认识Web.config文件Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式), ...

  7. 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)

    Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...

  8. Web.Config文件中使用configSource

    我们都知道,在asp.net中修改了配置文件web.config后,会导致应用程序重启,所有会话(session)丢失.然而,应用程序的配置信息放在配置文件里是最佳选择,在后台修改了配置后导致所有会话 ...

  9. .net中Web.config文件的基本原理及相关设置

    11.7  使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化  ...

随机推荐

  1. 安装完CentOS可以不做的事

    添加用户到sudo. 打开/etc/sudoers 找到root ALL=(ALL) ALL这一行,在后面再加上一行就可以了(不用引号): username ALL=(ALL) ALL 注意,都用ta ...

  2. django做form表单的数据验证

    我们之前写的代码都没有对前端input框输入的数据做验证,我们今天来看下,如果做form表单的数据的验证 在views文件做验证 首先用文字描述一下流程 1.在views文件中导入forms模块 2. ...

  3. 帧动画布局文件 animation-list

    <?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android=&qu ...

  4. Sql优化-必劳记!

    0. 尝试在合适的场景下,用 Charindex()函数代替 like,或者全文索引进行 内容搜寻.%like%不走索引,'like%'后百分号可以走索引. 1.调整不良SQL通常可以从以下几点切入: ...

  5. ubuntu系统下安装pyspider:安装命令集合。

    本篇内容的前提是你已安装好python 3.5.在ubuntu系统中安装pyspider最大的困难是要依赖组件经常出错,特别是pycurl,但把对应的依赖组件安装好,简单了.下面直接上代码,所有的依赖 ...

  6. 2018.10.14 loj#6003. 「网络流 24 题」魔术球(最大流)

    传送门 网络流好题. 这道题可以动态建图. 不难想到把每个球iii都拆点成i1i_1i1​和i2i_2i2​,每次连边(s,i1),(i2,t)(s,i_1),(i_2,t)(s,i1​),(i2​, ...

  7. 2018.07.04 POJ 1696 Space Ant(凸包卷包裹)

    Space Ant Time Limit: 1000MS Memory Limit: 10000K Description The most exciting space discovery occu ...

  8. 30. Child Labor Problem and Its Solution 童工问题及解决方法

    30. Child Labor Problem and Its Solution 童工问题及解决方法 ① Over a hundred years ago,Charles Dickens shocke ...

  9. dev ChartControl 备忘

    一个chartControl 里包括以个diagram(图表) diagram里可以设置 x-axis与y-axis ,另外还可以设置SecondaryXAxis与SecondaryYAxis,在Se ...

  10. aused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method fai

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleDaoImpl' ...