asp.net 数据库连接加解密

有时我们为了安全的原因,需要对配置的数据库连接进行加密,这边是asp.net网站的加解密方式。

1. 加密

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
}

 

2.解密

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section != null && section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}

 

加密后.net底层可以自动解密,对原有功能没影响。迁移到别的服务器估计是不可以,这个还没试过。

 

posted @ 2020-07-21 14:46  zzljh  阅读(452)  评论(0编辑  收藏  举报