asp.net web.config加密和解密
2012-12-14 16:16
477 查看
摘要:本文介绍asp.net web.config加密和解密,并提供简单的示例代码供参考。
web.config文件时一个XML文本文件,用来存储ASP.NET中WEB应用程序的配置信息。Web应用开发者往往会将应用程序的一些关键信息配置在web.config文件中,例如:数据库连接字符串。然而,web.config文件也存在一定的安全性隐患,这种以明码方式存储的关键信息,可能会成为别有用心人的突破口,造成不必要的损失。目前,解决这个问题最好的方法是将web.config文件的字符串进行加密,当要修改信息时再进行解密。下面我来介绍一种比较常用的web.config加密和解密方法。
实现过程。
(1)首先我们在页面中添加两个按钮,在加密按钮的单击事件中编写加密代码。(这里需要引用using System.Web.Configuration;命名空间)
// 打开该应用程序中的配置文件 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); //在打开的配置文件中找到指定的节 ConfigurationSection section = config.GetSection("appSettings"); if (section != null && !section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); config.Save(); Response.Write("<script>alert('加密成功!');</script>"); }
(2)在解密按钮的单击事件中编写解密代码。
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section = config.GetSection("appSettings"); if (section != null && section.SectionInformation.IsProtected) { section.SectionInformation.UnprotectSection(); config.Save(); Response.Write("<script>alert('解密成功');</script>"); }
效果图如下:
加密前:
加密后:
解密后:
相关文章推荐
- 利用ASP.NET加密和解密Web.config中连接字符串
- (译)利用ASP.NET加密和解密Web.config中连接字符串
- 说一说ASP.NET web.config 加密及解密方法 (代码)
- 利用ASP.NET加密和解密Web.config中连接字符串
- asp.net web.config加密解密方法
- asp.net web.config加密解密方法
- asp.net web.config加密和解密
- ASP.NET web.config中,数据库连接字符串的加密与解密
- ASP.NET 2.0加密解密Web.config配置文件
- asp.net 在webConfig中对某个节点加密和解密
- ASP.NET 程序安全性 (一) web.config加密与解密
- 利用ASP.NET加密和解密Web.config中连接字符串
- asp.net Web.config加密解密
- ASP.NET温故而知新学习系列之网站安全技术—Web.config加密和解密(一)
- ASP.NET 程序安全性 (一) web.config加密与解密
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET中web.config中加密连接字符串 [转自9who]
- 采用ASP.NET IIS 注册工具 (Aspnet_regiis.exe)对web.config实行本地加密
- ASP.NET 2.0加密Web.config 配置文件
- ASP.NET应用程序中加密Web.config 转载