大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > ASP.NET技巧 > ASP.NET对WebConfig文件加密源码

ASP.NET对WebConfig文件加密源码

关键词:ASP.NETC#WebConfig加密,源码  阅读(1114) 赞(57)

[摘要]配置文件(Web.Config)加密解密代码

首先引用命名空间:


using System.Configuration;
using System.Web.Configuration;
加密解密核心函数:



     /// <summary>
    /// 加密appSettings
    /// </summary>
    private void BindappSettings()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section = config.GetSection("appSettings");
        if (section != null && !section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            config.Save();
        }
    }
    /// <summary>
    /// 加密链接字符串
    /// </summary>
    private void BindconnectionStrings()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section = config.GetSection("connectionStrings");
        if (section != null && !section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            config.Save();
        }
    }
   
    /// <summary>
    /// 解密连接字符串
    /// </summary>
    private void ChooseappconnectionStrings()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section = config.GetSection("connectionStrings");
        if (section != null && section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            config.Save();
        }
    }
    /// <summary>
    /// 解密appSettings
    /// </summary>
    private void ChooseappSettings()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section = config.GetSection("appSettings");
        if (section != null && section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            config.Save();
        }
    }





相关评论