Pages

Monday, November 14, 2011

Encrypt Appsettings in web.Config


Add these below two lines in pageload event:
ProtectSection("appSettings","DataProtectionConfigurationProvider");
UnProtectSection("appSettings");


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 executing this just go to web.config file and check the app settings tag.

No comments:

Post a Comment