Encrypting and Decrypting Configuration Section in web.config file

No.of Views1839
Bookmarked0 times
Downloads 
Votes2
By  Kirti.M.Darji   On  15 Feb 2010 22:02:11
Tag : ASP.NET , How to
Encrypting and Decrypting Configuration Section in web.config file
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 


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 in Aspx Page like

Copy-Past following code in aspx page


{codecitation class="brush:html; gutter: true;" width="650px"}




DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">





Untitled Pagetitle>

head>








onclick="btnEncrypt_Click" />
runat="server" Text="Dycrypt" onclick="btnDyscrypt_Click" />

div>

form>

body>

html>


{/codecitation}

Step-2

Copy-past following two method in code file (aspx.cs) of Page

{codecitation class="brush: csharp; gutter: true;" width="650px"}

protected void btnEncrypt_Click(object sender, EventArgs e)

{

try

{

Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection confStrSect = confg.GetSection("connectionStrings");

if (confStrSect != null)

{

confStrSect.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

confg.Save();

}

}

catch (Exception ex)

{

throw ex;

}

}

protected void btnDyscrypt_Click(object sender, EventArgs e)

{

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)

{

}

}
{/codecitation}




Conclusion
We can easily encrypt web.config file at client place for some confidential information.

Enjoy!!!!!!

About the Author


Kirti Darji
P.G.D.C.A, M.SC(Computer Science) 3+ YEAR EXPERIENCE Visual C#.net and VB.net Windows and Web base Application

Expertise :C#.net, VB.Net,ASP.net,SQL Server, MS Access,aJAX, JavaScript,CSS,HTML,XML,N TIRE ARCHITECTURE,oOPS,Web Services,Core Ajax,visual Source safe,IIS

Occupation :SR. Software Developer
Company : MADHUVAN INFO TECH PVT.LTD
Location :AHMADABAD

 
Sign Up to vote for this article
 
About Author
 
Kirti.M.Darji
Occupation-Software Engineer
Company-Maven-Infosoft Pvt.Ltd
Member Type-Expert
Location-Not Provided
Joined date-03 Jun 2009
Home Page-
Blog Page-http://kirtimdarji.blogspot.com/
I am Kirti M. Darji Senior Software Developer Having 3.5 Years Experience In Microsoft .Net(c#) Technologies.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top