5,427,303 members and growing! (15,504 online)
Email Password   helpLost your password?
Web Development » Web Security » Security     Intermediate

Enhanced and Secure Connection Strings in Web.Config

By Vasudevan Deepak Kumar

Here we would discuss some simple steps, which would facilitate keeping our database connection strings safe and encrypted in Web.Config.
C#, VB.NET 1.0, Win2K, WinXP, Windows, .NET, ASP.NET, Visual Studio, Dev

Posted: 25 Jan 2003
Updated: 25 Jan 2003
Views: 95,035
Bookmarked: 50 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
27 votes for this Article.
Popularity: 3.60 Rating: 2.51 out of 5
10 votes, 37.0%
1
3 votes, 11.1%
2
2 votes, 7.4%
3
11 votes, 40.7%
4
1 vote, 3.7%
5

Introduction

In developing ASP.NET applications, we make heavy use of Web.Config to store and retrieve database connection strings. But we need to be aware of the fact that Web.Config is a simple XML text file and its contents are readable by any user having access to the the webserver's file system. Albeit the fact that accesses like http://localhost/deepak/web.config etc. are halted by the webserver with a message 'The type of page is not served', anybody with console access to the system can still open and read the database connection strings, which might contain the password to the database in an unencrypted manner.

Simple encryption

After a great deal of search in MSDN and other sites, I found a simple way to encrypt strings using a minimum of 8 character string (8 characters should be okay since even MSN Hotmail recommends a minimum of 8 character passwords for all accounts). Sections have been taken from ASPAlliance example but the method has been kept as a static method for the simple reason that you need not create object for every encryption and decryption strategy.

The attached example makes use of DESCryptoServiceProvider that is available in System.Security.Cryptography namespace.

Enhancement

For example sake, I have given both the key and the encrypted string in web.config. But for security reasons, it would be advisable to keep the key elsewhere in the file system and read the key dynamically from this file from the specified location. Additional care has to be taken that the place where we store the key is accessible only to System Administrators and other authorized personnel. With this strategy and trick in place, the database connection string could be made relatively safe for a particular web application.

How to use the example

Include the following two lines in web.config:

         <add key="cKey" value="LavanyaDeepak"/>
         <add key="cDb" value="C0AHny7FDFewTPE7eTp5RA=="/>

To any of your test applications, unzip the files in the archive (Cryptography.cs and Test.Aspx and Test.Cs). Include them in a project in Visual Studio .NET. Build the application and run test.aspx from the web browser.

Conclusion

I hope the above article would be very useful for .NET developers worldwide to make effective and secure use of database connection strings that are put in Web.Config. Many thanks to developers whose ideas and pieces of code have been helping me out in drafting these static methods.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vasudevan Deepak Kumar


Vasudevan Deepak Kumar is from Chennai, India who has been in the programming career since 1994, when he was 15 years old. He has his Bachelors of Engineering (in Computer Science and Engineering) from Vellore Engineering College (now VIT University). He also has a MBA in Systems from Alagappa University, Karaikudi, India.


He started his programming career with GWBasic and then in his college was involved in developing programs in Fortran, Cobol, C++. He has been developing in Microsoft technologies like ASP, SQLServer 2000. For sometime, he has also been with PHP and MySQL based development in one of his previous organizations. Now currently his focus is on Microsoft .NET World (ASP.NET, C# and Whidbey)


In his past-time, he listens to polite Carnatic Music.

Web Presence



Homepage

http://www.lavanyadeepak.tk/

Blogs



Technical




Gossips




Spiritual







Occupation: Web Developer
Location: India India

Other popular Web Security articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralDeveloper Caste Systemmembershitba6:12 13 Jan '06  
GeneralThe encryption codememberdurayakar17:41 12 Nov '03  
GeneralRe: The encryption codememberVasudevan Deepak Kumar17:47 12 Nov '03  
Generalhmm ...memberkrumpo1:52 28 Jan '03  
GeneralRe: hmm ...memberEnki4215:10 30 Jan '03  
GeneralRe: hmm ...memberDeepak Kumar Vasudevan17:42 30 Jan '03  
GeneralRe: hmm ...memberEnki422:22 31 Jan '03  
GeneralRe: hmm ...memberThePhoenix23:20 8 Jul '03  
GeneralRe: hmm ...sussAnonymous11:27 22 Feb '05  
GeneralRe: hmm ...sussAnonymous8:19 12 Oct '05  
GeneralRe: hmm ...membershahprabal8:26 4 Jan '06  
GeneralCool IdeamemberHeath Stewart5:56 26 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jan 2003
Editor: Smitha Vijayan
Copyright 2003 by Vasudevan Deepak Kumar
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project