Sunday, July 25, 2010

Encrypting and Decrypting Configuration Section in web.config file

Introduction

This article explains about how to encrypt and Decrypt web.config file in web application. In web Application Sometime we put come confidential information in configuration section like (appsetting, Connectionstring)

Technologies

.NET Framework 2.0 or later

Language

C#

Prerequisite

Visual Studio 2005 and Later

Implementation

Following namespace will added
using System.Web.Configuration;

Step-1

Take two buttons name like btnEncrypt and btnDyscrypt  in aspx Page

Step-2

Copy-past bellow in btnEncrypt Click Event


try
{
Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection confStrSect = confg.GetSection("connectionStrings");
if (confStrSect != null)
{
onfStrSect.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

confg.Save();

}
}
catch (Exception ex)
{
throw
}

Copy-past bellow in btnDyscrypt Click Event

try

{

Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection confStrSect = confg.ConnectionStrings; 
if (confStrSect != null && confStrSect.SectionInformation.IsProtected)
{
confStrSect.SectionInformation.UnprotectSection();
confg.Save();
}
}
catch (Exception ex)
{ 
throw
}
Conclusion

This is My First article on my blog  so any comment related to writing on blog are acceptable.so we can easily encrypt web.config file at client place for some confidential information.

Happy Coding!!!