web.config 节点的加密

 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

你可能感兴趣的:(web.config 节点的加密)