Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Encrypt & Decrypt ConnectionString Section

0.00/5 (No votes)
22 Aug 2012 1  
Encrypt & Decrypt ConnectionString SectionSometimes we need to secure ConnectionString to prevent anyone can knows it. whatever your purpose from

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Encrypt & Decrypt ConnectionString Section

Sometimes we need to secure ConnectionString to prevent anyone can knows it. whatever your purpose from securing ConnectionString, there is a way to Encrypt and Decrypt ConnectionString by special codes as we will see now...

In the Web.Config file we found <connectionStrings> section that enable us to add ConnectionStrings

<connectionStrings>

  <add name="ConnectionString" connectionString="Provider=SQLNCLI10.1;Data 

Source=My-pc\sqlexpress;Integrated Security=SSPI;Initial Catalog=Northwind"

   providerName="System.Data.OleDb" />

 </connectionStrings>

Now we have to go to see how can we Encrypt and Decrypt ConnectionStrings don't foreget add System.Web.Configuration name space

 protected void Encryption(bool EncryptoValue)

    {

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

        ConfigurationSection sec = config.GetSection("connectionStrings");

        if (EncryptoValue == true)

        {

            sec.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

        }

        else

        {

            sec.SectionInformation.UnprotectSection();

        }

        config.Save();

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Encryption(true);

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        Encryption(false);

    }

You can test that now hope that useful

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here