有时我们为了安全的原因,需要对配置的数据库连接进行加密,这边是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底层可以自动解密,对原有功能没影响。迁移到别的服务器估计是不可以,这个还没试过。

 

相关文章:

  • 2022-01-04
  • 2021-10-27
  • 2022-12-23
  • 2021-04-17
  • 2022-01-13
  • 2022-12-23
  • 2021-12-22
猜你喜欢
  • 2021-11-13
  • 2021-10-24
  • 2021-08-20
  • 2022-01-16
相关资源
相似解决方案