Caveman's Blog

My commitment to learning.

Encrypting the Web.Config configurations

with one comment

The web.config is a file that is not accessible to the user of a web application via a browser. The dotnet framework shields this file from all kinds of external intrusion. However someone having access to the physical file can steal sensitive information.

This vulnerability can be overcome by using aspnet_regiis, an in-build ASP.Net tool.  Following these simple steps, sections of the web.config can be encrypted (and/or decrypted) to secure the configuration settings:

Step 1: Encrypt connection Strings in the web.config. This command uses RSAProtectedConfigurationProvider (uses TripleDES and RSA encryption) for encrypting sections of the web.config file. This command has to be executed in a Visual Studio 2005/2008 command prompt window at the location where the web.config resides:

aspnet_regiis -pef connectionStrings .

Step 2: Adding users to the ACL of the key containers using the –pa parameter. Give access to the ASP.NET user:

aspnet_regiis -pa “NetFrameworkConfigurationKey” “ASPNET”

Step 3: Decrypt Connection Strings to clear text

aspnet_regiis -pdf connectionStrings .

The best part is that, the web application code does not have to change one bit after the encryption of web.config file. Following is a before and after view of the Connection Strings section of a web.config file:


  <add name="MembershipConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LOCALDB.MDF;Integrated Security=True;User Instance=True"
  providerName="System.Data.SqlClient" />


<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
  <EncryptedData Type=""
    <EncryptionMethod Algorithm="" />
    <KeyInfo xmlns="">
      <EncryptedKey xmlns="">
        <EncryptionMethod Algorithm="" />
        <KeyInfo xmlns="">
          <KeyName>Rsa Key</KeyName>

Tip: Here is how to secure appsettings

Encrypt appSettings: aspnet_regiis -pef appSettings .

Decrypt appSettings: aspnet_regiis -pdf appSettings .

1. How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA
2. Encrypting configuration files using protected configuration


One Response

Subscribe to comments with RSS.

  1. Thanks for your articles about multiple config files and web.config encryption. Now, the question is how to encrypt a section, located in separated config file.


    October 1, 2009 at 1:17 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: