AppSettings Encryption in web.Config file


Some time we need to encrypt the web.config for the Security reason's and for acheving that we need to add below mentioned two lines in pageload event:

ProtectSection("appSettings","DataProtectionConfigurationProvider");
UnProtectSection("appSettings");


After that add these methods

private void ProtectSection(string p, string p_2)
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);


        ConfigurationSection section = config.GetSection(p);


        if (section != null && !section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection(p_2);
            config.Save();
        }
    }


    private void UnProtectSection(string sectionName)
    {
        Configuration config =
            WebConfigurationManager.
                OpenWebConfiguration(Request.ApplicationPath);


        ConfigurationSection section =
                  config.GetSection(sectionName);


        if (section != null &&
              section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
            config.Save();
        }
    }


After execution just see find the changes in web.config look for app settings tag.

Comments

Technology