您的位置:首页 > 其它

web.config 节点的加密

2009-10-31 00:06 399 查看
web.config 节点的加密

using System.Web;
using System.Web.Configuration;
using System.Configuration;
using System;

namespace King
{
public class ProtectedWebConfigs
{
public bool ProtectedWebConfig()
{
return false;
}
public bool ProtectedWebConfig(string sectionName)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
string provider = "RsaProtectedConfigurationProvider";
if (!config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.ProtectSection(provider);
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool ProtectedWebConfig(string sectionName, string path)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
string provider = "RsaProtectedConfigurationProvider";
if (!config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.ProtectSection(provider);
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool UnProtectedWebConfig(string sectionName)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
if (config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.UnprotectSection();
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
public bool UnProtectedWebConfig(string sectionName, string path)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
if (config.GetSection(sectionName).SectionInformation.IsProtected)
{
config.GetSection(sectionName).SectionInformation.UnprotectSection();
try
{
config.Save();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return true;
}
}
}
}

命令行加密方法
aspnet_regiis -pe (加密的节,如connectionString) app /虚拟目录
命令行解密方法
aspnet_regiis -pd (加密的节) app /虚拟目录
RsaRrotectedConfigurationProvider
2009-10-31-00:08:20
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: