Monday, 9 May 2016

Encrypt Web config's section for ASP.NET Website

Hi guys, Welcome back, i was reading how to make website more secure, while studding i found, we can encrypt the sections of web.configs. i thought it is an interesting topic to do some R&D on. i started to work on it and now i am able to encrypt sections of web.config file.

Lets start to encrypt the web.config sections, i am encrypting to connectionString section in this article.

Steps to do a simple demo:

1. Create a new Website
2. add connectionstring element into it and connect to any DB.
3. open "Developer Command Prompt for Visual Studio" as administrator privilege.
4. change directory of command prompt to specific version of .net in which your website is as for this example i have created website in 4.5 version:

so i have changed the command directory to


5. add the following section into web.config under configuration tag:

<configProtectedData defaultProvider="SampleProvider">
  <add name="SampleProvider" 
          System.Configuration, Version=, Culture=neutral, 
    useMachineContainer="true" />
6. Now in command prompt run the following command

aspnet_regiis -pef "connectionStrings" "E:\New folder\WebApplication2\WebApplication2"

7. now check your web.config file, you will find the connectionString section is changed to some identical format as given below:

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

that's all..Now no body will come to know what is the DB server what is the DB name and what are the creds.

And another Good thing, do are not supposed make any changes in connecting to DB from code. connect it as we do normally, no extra efforts are needed.

BBYE for Now.. will come back with any new article. till then Happy Coding...!!!!