//对 web.config 的加密解密

//如果要加密 appSettings 把 connectionStrings 改为 appSettings 就可以

<appSettings>
 <add key="con" value="server=.;uid=sa;pwd=;database=demo"/>
</appSettings>
<connectionStrings>
 <add name="con" connectionString="server=.;uid=sa;pwd=;database=demo" />
</connectionStrings>

//DPAPI加密解密
protected void btnPdfPefDPAPI_Click(object sender, EventArgs e)
{
 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
 ConfigurationSection configSection = config.GetSection("connectionStrings");
 //如果已经加密,则进行解密
        if (configSection.SectionInformation.IsProtected)
        {
            configSection.SectionInformation.UnprotectSection();
            config.Save();
        }
 //加密
        else
        {
            configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            config.Save();
        }
}


//RSA加密解密
protected void btnPdfPefRSA_Click(object sender, EventArgs e)
{
 Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
 ConfigurationSection configSection = config.GetSection("connectionStrings");
 ////如果已经加密,则进行解密
        if (configSection.SectionInformation.IsProtected)
        {
            configSection.SectionInformation.UnprotectSection();
            config.Save();
        }
 //加密
        else
        {
            configSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
            config.Save();
        }
}

 

--蛮基础的一些东西,备忘用的.

相关文章:

  • 2022-12-23
  • 2021-12-31
  • 2021-09-14
  • 2021-07-05
  • 2021-10-27
  • 2022-12-23
  • 2022-02-16
猜你喜欢
  • 2021-09-06
  • 2022-01-13
  • 2022-02-15
  • 2021-11-12
  • 2021-11-28
  • 2021-06-07
  • 2022-12-23
相关资源
相似解决方案