locked
Protect the Configuration File RRS feed

  • Question

  • 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

    Wednesday, April 2, 2008 10:31 AM

All replies

  • User1439985827 posted

    I'm sorry, but I don't understand or see a question. Are you asking a question, or are you providing the forums with a tip or trick? 

    Wednesday, April 2, 2008 10:59 AM
  • User190023156 posted

    Why would you need to protect your web.config file? it is only accessible locally anyway is it not? and as i understand it cant be changed programatically?

    Wednesday, April 2, 2008 11:46 AM
  • User-1137493631 posted

    Hi guys,

    It's only for a trick to protect our webconfig file, not a question.And why not is it not possible .it is possible to change the web config file programaticly.

    Thursday, April 3, 2008 12:26 AM