Click here to Skip to main content
15,879,326 members
Articles / General Programming / File
Article

Encrypt & Decrypt ConnectionString Section

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 9.1K   2   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
QuestionPlease Fix your Code Sample Format Pin
bjhuffine26-Jan-18 2:46
bjhuffine26-Jan-18 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.