Click here to Skip to main content
11,920,211 members (49,825 online)
Click here to Skip to main content
Add your own
alternative version


120 bookmarked

Encryption of Connection Strings Inside the Web.config in ASP.NET 2.0

, 1 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Encryption & Decryption of Connection Strings inside the Web.config in ASP.NET 2.0


This article gives you a general view of the Encryption of Connection string inside the Web.config feature and how you can use encryption and increase the security and keep the secure connection with the database.

About Encryption & Decryption of Connection Strings

The .NET Framework 2.0 allows you to encrypt configuration sections within the Web.config or machine.config files.

Encryption support for configuration files is added to the .NET Framework 2.0. The .NET Framework libraries support encryption and decryption in code. In this article, I'll show how to protect data stored in a configuration file via encryption and describe configuration file sections.

It is recommended that you store your database connection strings in the Web.config file and encrypt the connection strings.

This feature allows developers to encrypt one or more sections of a configuration file. The following sections for encryption:

  • connectionStrings: Database connection strings
  • appSettings: Custom application settings
  • sessionState: Configures session state
  • Identity: Web application identities, which may include impersonation credentials

We can't use the Protected Configuration feature on the following sections of web.config and machine.config files:

  • processMode
  • runtime
  • mscorlib
  • configProtectedData
  • satelliteassemblies
  • cryptographySettings
  • cryptoNameMapping
  • cryptoClasses

Protect Sensitive Data

Encryption of configuration feature improves application security. If anybody can access the web.config file, then they can't access the database information. The .NET Framework provides two ways to encrypt configuration files:

  • The aspnet_regiis.exe command-line utility
  • Encryption within developers application code

This article focuses on the application code approach.

The following namespaces are used to encrypt configuration files for code approach:

  • System.Configuration
  • System.Web.Configuration

It contains the following two methods associated with encryption:

  • ProtectSection: Marks a configuration section for protection. The name of the provider to be used for the encryption and it is passed to the method as its only parameter
  • UnprotectSection: Removes the protected encryption from the associated configuration section


The following simple ASP.NET web.config file demonstrates encryption and decryption of configuration data.

Step 1: View the connectionStrings web.config Section

<connectionstrings><add class=""code-string"" name=""<span"">
"Conn" connectionString="Data Source=manish;Initial Catalog=Publish;
User ID=sa;Password=admin"
providerName="System.Data.SqlClient" /> </connectionstrings>

Step 2: Imports Following Namespace

We will write the code where we call the connection string or initialize the connection string.

Imports System.Configuration
Imports System.Web.Configuration

Step 3 : Create a Function (configencryption)

This function works for encryption and decryption.
The following VB.NET code from an ASP.NET Web form encrypts the connectionStrings section of the configuration file:

Public Shared Function webencrypt()
        Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        '' Write the section name of web.config file (connectionStrings)
        Dim configSection As ConfigurationSection = c_

        '' Check the section  of web.config file (connectionStrings) if Protected 
        '' then UnprotectSection (decrypt) the section
        If configSection.SectionInformation.IsProtected Then
            '' Check the section of web.config file (connectionStrings) 
            '' if UnprotectSection then 
            '' protect (encrypt) Section the section
        End If
    End Function 

Step 4: Call this Function Before the Connection Initialize on *.vb File

Call webencypt()
sqlstring = System.Configuration.ConfigurationManager.ConnectionStrings_

After you run the above code and the web.config has been encrypted, you can open up the web.config file in your ASP.Net project. The contents of the web.config section will now appear encrypted.

Step 5: View the connectionStrings web.config Section (decrypted)

<connectionstrings class=""code-string"" 
PbubNuVgcueSdH8bdHJXSc1hucDURrFAAAAHOaFiNRTxD+d8YctPO/HiGD9NeV  </cipherdata>

Step 6: When Next Time Page Post Back then View the connectionStrings web.config Section

<connectionstrings><add class=""code-string"" name=""<span"">
"Conn" connectionString="Data Source=manish;Initial Catalog=Publish;
User ID=sa;Password=admin"
providerName="System.Data.SqlClient" />


In this article, we saw how to encrypt and decrypt the connection strings section in ASP.NET 2.0 web.config files.


This article is purely for educational purposes and is a compilation of notes, material and my understanding on this subject.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish K Gupta
Software Developer (Senior) Diaspark
India India
I am Manish Gupta, i m Software Developer

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Viper20109-Sep-13 15:37
memberViper20109-Sep-13 15:37 
GeneralMy vote of 5 Pin
Member 888949020-Jul-12 4:03
memberMember 888949020-Jul-12 4:03 
Questionfor web config file Pin
Gavhale7-Feb-12 21:46
memberGavhale7-Feb-12 21:46 
GeneralNice code, Pin
javier.alpizar1-Jul-10 8:55
memberjavier.alpizar1-Jul-10 8:55 
GeneralRe: Nice code, Pin
Manish K Gupta4-Jul-12 22:06
memberManish K Gupta4-Jul-12 22:06 
GeneralMy vote of 2 Pin
zakm7-Jun-10 2:05
memberzakm7-Jun-10 2:05 
GeneralIt only works in development and not in an elegant way Pin
DimitrisGr23-Feb-09 23:54
memberDimitrisGr23-Feb-09 23:54 
GeneralGood idea, try combining it with a facade pattern Pin
Brett Slaski9-Feb-09 6:22
memberBrett Slaski9-Feb-09 6:22 
QuestionHow secure is it? Pin
Qistoph2-Feb-09 21:57
memberQistoph2-Feb-09 21:57 
QuestionAny C# Code ? Pin
Pankajkumar Nikam2-Feb-09 4:17
memberPankajkumar Nikam2-Feb-09 4:17 
AnswerRe: Any C# Code ? Pin
sumit70342-Feb-09 6:40
membersumit70342-Feb-09 6:40 
GeneralRe: Any C# Code ? Pin
Pankajkumar Nikam2-Feb-09 8:18
memberPankajkumar Nikam2-Feb-09 8:18 
GeneralNice Pin
Vuyiswa Maseko2-Feb-09 0:36
memberVuyiswa Maseko2-Feb-09 0:36 
Generalgood article Pin
Donsw12-Jan-09 4:00
memberDonsw12-Jan-09 4:00 
Generalnice trick Pin
ArpitDubey11-Nov-08 8:17
memberArpitDubey11-Nov-08 8:17 
General[Message Removed] Pin
hankjmatt14-Oct-08 0:51
memberhankjmatt14-Oct-08 0:51 Pin
ramesh_pune131-May-08 21:29
memberramesh_pune131-May-08 21:29 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 2 Feb 2009
Article Copyright 2008 by Manish K Gupta
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid