Encrypt and Decrypt any section of config file


On the below code I am encryting and decrypting the ConnectionString section of Web.Config
Here "DataProtectionConfigurationProvider" is just a type of protection level.

Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");


ConfigurationSection section = objConfig.GetSection("connectionStrings");
            if (!section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                objConfig.Save();

            }

            if (section.SectionInformation.IsProtected)
            {

                section.SectionInformation.UnprotectSection();
                objConfig.Save();

           
            }

No comments:

Post a Comment