Friday 15 December 2017

Encrypt And Decrypt ConnectionString in Web.Config File

Here We are going to see how we can encrypt and secure our connection string in our web config file. As you all know the connection string and other important information section(like smtp details) in web.config is the pillar of our data. I mean, without a connection string you just can't create an application which does some database actions like retrieving data, creating data. Here I am going to show you a demo of how we can do that, You can do the same thing in your Web API, Asp MVC, Asp.net project. I Hope you will like this.

Agenda

  • Add Connection String in Web.config
  • Encrypt the connection string
  • Decrypt the connection string
Prequisites
  • Visual Studio
  • SQL Server
Add a connection String in Web.config
The connection string property must be placed under configuration section in web,config file. Here is Mine.

<connectionStrings>
       <add name="myConnection" connectionString="Data Source=BRIJ\SQLEXPRESS;Initial Catalog=ReportServer$SQLEXPRESS;Integrated Security=True" />
</connectionStrings>

Encrypt connection string
To start the Process, you must open your command window with the admin privilege. the type the following command

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 

This Command will narrate you to the framework version folder. Now Explore Project Folder Containing Folder and then copy the location. For me it is F:\TestWebApplication. Now please go back to your command propmt and type the command as follows.

ASPNET_REGIIS -PEF "connectionStrings" "F:\TestWebApplication"

After Executing Above Command in cmd You Will get Succeede Message on you cmd.
Now Check you Web.config File.
If You want to Encrypt Some Section Like smtp Protocol details. You can also it. You have to Replace "connectionStrings" with "system.net/mailSettings/smtp" and Execute Command.

ASPNET_REGIIS -PEF "system.net/mailSettings/smtp" "F:\TestWebApplication"

Decrypt connection string
You can always decrypt the connection string if you want, to decrypt just need follows the command as in the command prompt.

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 

once after you did you the above command you can execute the preceding one.

ASPNET_REGIIS -PDF "connectionStrings" "F:\TestWebApplication"

the text 'connectionStrings' is case sensitive. After Executing Command Successfully you will get your connectionStrings into Readable Version. My Decrypted connectionStrings like

<connectionStrings>
       <add name="myConnection" connectionString="Data Source=BRIJ\SQLEXPRESS;Initial Catalog=ReportServer$SQLEXPRESS;Integrated Security=True" />
</connectionStrings>

You can Decypt Smtp Section using below Command

ASPNET_REGIIS -PDF "system.net/mailSettings/smtp" "F:\TestWebApplication"

No comments:

Post a Comment

Youtube blocked hack play youtube video from embedded url

Youtube introduced new feature for playing video in own website or third party application using embed feature. Youtube blocked in some of ...