How to Encrypt a Section in Web.Config?

It is often required to encrypt usernames, passwords or even connection string in the web.config file. The usual method to do is using any of the encryption algorithm and saving the encryption Key in the config file. .NET has provided with a classic feature of encrypting an entire section in the web.config file. Using this feature .NET saves the key in the Machine.key file. User ONLY needs to encrypt the section in the web.config file, the decryption of the section is taken care by the .NET framework. Here is a step by step procedure on encrypting a section:

Section in Web.config

<!-- User Credentials -->
<add key ="domain" value ="domain_name"/>
<add key ="username" value ="user_name"/>
<add key ="password" value ="password"/>

Step 1:
Open the Visual Studio Command Prompt in Administrative Mode
Go to Start –> Programs –> Visual Studio 2008 –> Visual Studio Tools –> Visual Studio Command Prompt right click and say “Run as Administrator

Step 2:
Type the following command:
aspnet_regiis -pef “ImpersonateUser” “D:\SourceCode\RootFolder” -prov “RsaProtectedConfigurationProvider
The web.config file should be present at the path “D:\SourceCode\RootFolder”. The actual command looks like:

Step 3:
Run the above command.
The above command will encrypt the ImpersonateUser section in the web.config file and will save the web.config file at the given location. The encrypted section will look like:

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

Most important thing is, while using the web.config key’s in the C# code we DO NOT have to decrypt the section. .NET automatically does it and provides us with the decrypted values.

Hope this helps. Your comments will help us improve 🙂


3 Responses to “How to Encrypt a Section in Web.Config?”

  1. James Says:

    I have followed the steps you have mentioned above. It works great on the developer’s environment, but throws an error in production. The error says – Unable to access RSA configuration.

    Can you please provide the solution?

  2. Sandeep Aparajit Says:

    It seems that your ASP.NET or the Network Service account does not have the required premission. Use the following command:
    aspnet_regiis -pa “NetFrameworkConfigurationKey” “ASPNET”
    aspnet_regiis -pa “NetFrameworkConfigurationKey” “NT Authority\Network Service”.

    Hope this helps you 🙂

  3. Offshore Software Development India Says:

    Hey Sandeep m having the permission. And its working well. Exactly what I needed.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: