Thursday, 15 November 2012

Encrypt Your Web.Config


Encrypt Your Web.Config


This will tell you how to encrypt your web config sections(Connection,Appsetting etc).

Step 1:

Include the below Namespace:

using System.Web.Configuration;

Step 2:


protected void btnEncript_Click(object sender, EventArgs e)

    {

        //.Net provides to Encription

        //1.RSAProtectedConfigurationProvider

        //2.DataProtectionConfigurationProvider

        ProtectSection("connectionStrings",

        "DataProtectionConfigurationProvider");

    }



Step 3:

<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">ConfigurationSection section;</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            Configuration config = WebConfigurationManager.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                    OpenWebConfiguration(Request.ApplicationPath);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">             section =</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                         config.GetSection(sectionName);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">             if (section != null &&</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                       !section.SectionInformation.IsProtected)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">             {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                 section.SectionInformation.ProtectSection(provider);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">                 config.Save();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">             }</div>

private void ProtectSection(string sectionName, string provider)
    {
        ConfigurationSection section;
            Configuration config = WebConfigurationManager.
                    OpenWebConfiguration(Request.ApplicationPath);


             section =
                         config.GetSection(sectionName);


             if (section != null &&
                       !section.SectionInformation.IsProtected)
             {
                 section.SectionInformation.ProtectSection(provider);
                 config.Save();
             }
  
    }

The above code is self description.
the above code will be used to Encrypt the Webconfig "connectionstring" section.

Step 4:

protected void btnDecript_Click(object sender, EventArgs e)
    {
        UnProtectSection("connectionStrings");
    }

Step 5:

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();
        }
    }

The above code will be used to decrypt the Webconfig "Connectionstring" Section.
here i have used this code in a button click because of easy understanding and implementation.

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top