Code:
public void ProtectWebConfig(object sender, EventArgs e)
{
Configuration config1 =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section1 = config1.GetSection("mailSettings");
if (section1 != null && !section1.SectionInformation.IsProtected)
{
section1.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config1.Save();
}
Configuration config2 =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section2 = config2.GetSection("connectionStrings");
if (section2 != null && !section2.SectionInformation.IsProtected)
{
section2.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config2.Save();
}
}