User-1137493631 posted
Hi,
A sample Code for Protect Web.config file.
1.Create New Website
2.Add New Page
3.Place the Two Buttons and named as "ProtectConfig" and "UnProtectConfig"
4.In UnProtectConfig's Click Event write the following code
protected void ProtectConfig_Click(object sender, EventArgs e)
{
ProtectConfiguration("appSettings","DataProtectionConfigurationProvider");
}
Here the "appSettings" means we have to protect the webconfig's appSettings sections
and "DataProtectionConfigurationProvider" is the provider to Protect the Configuration file.
4.In ProtectConfig's Click Event write the following code
protected void UnProtectConfig_Click(object sender, EventArgs e)
{
UnProtectConfiguration("appSettings");
}
Here the "appSettings" means we have to UnProtect the section
4.And then add the Following User Defined Functin for Protect and Unprotect
private void ProtectConfiguration(string sectionName,string provider)
{
Configuration config =WebConfigurationManager. OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if ( !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
}
This Function for Protect the Configuration section
private void UnProtectSection(string sectionName)
{
Configuration config =WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if ( section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
This Function for UnProtect the Configuration section