C#使用Configuration类对Web.Config节点进行加密解密
以下是加密和解密的方法:
private void EncryptConfig(bool bEncrypt)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");//Request.ApplicationPath
ConfigurationSection configSection = config.GetSection("connectionStrings");//configSection这样加密configSection
if (bEncrypt)
{
if (!configSection.SectionInformation.IsProtected) //如果已经加密过,就不再加密了
{
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");//加密
}
}
else
{
if (configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.UnprotectSection(); //解密
}
}
config.Save();
}
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");//Request.ApplicationPath
ConfigurationSection configSection = config.GetSection("connectionStrings");//configSection这样加密configSection
if (bEncrypt)
{
if (!configSection.SectionInformation.IsProtected) //如果已经加密过,就不再加密了
{
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");//加密
}
}
else
{
if (configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.UnprotectSection(); //解密
}
}
config.Save();
}
ASP.NET 在处理 Web.config 文件时会自动对该文件的内容进行解密。因此,不需要任何附加步骤即可对已加密的配置设置进行解密,供其他 ASP.NET 功能使用或用于访问代码中的值。
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号