473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Securing dB connection string in Web.config

Hi:

I'm storing my dB connection in web.config file. Since it will be easily
read by opening file, what is a good way to secure it?

Thanks,
Charlie
Nov 18 '05 #1
4 6711
The best technique is to use a trusted connection. That way you don't need
to list a username or password so there is nothing to hide.
If this is not possible, you can store the username and password encrypted
in the registry.
Here's more information:
http://msdn.microsoft.com/library/de...itysection.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi:

I'm storing my dB connection in web.config file. Since it will be easily
read by opening file, what is a good way to secure it?

Thanks,
Charlie

Nov 18 '05 #2
There is a good article on this in this month's edition of ASP.NET Pro. You
have to subscribe or buy the magazine though. The title of the article is
"Password in Web.config?"

http://www.aspnetpro.com/

-John Oakes
"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi:

I'm storing my dB connection in web.config file. Since it will be easily
read by opening file, what is a good way to secure it?

Thanks,
Charlie

Nov 18 '05 #3
First let me say, if you want real security, you will have to pay for it,
most of the low cost providers will let you have it but will charge you for
it. Otherwise you me need to find another provider. The same people that can
get at your web.config to read the password to your database will likely
already have access to your database with out your password. (Did that make
sense? It's so hard to tell)

That being said, I am in the same situation with my provider, so at the risk
of being chastised, here is what I have done: (I don't store any of this in
the Web.config, I use XML serialization to store it in it's own XML file)

I have a class that handles the database connection string, and it stores
the critical data (user name and password in an encrypted byte array) I have
overridden ToString() to give me the whole connection string when I ask for
it with the parts un-encrypted. Some thing like this:

public class DatabaseConnection
{
private byte[] _userid;
private byte[] _password;

public byte[] UserID
{
get { return _userid; }
set { _userid = value; }
}
public byte[] Password
{
get { return _password; }
set { _password = value; }
}
public string SetUserID(string user)
{
UserID = Globals.Encrypt(user);
}
private string GetUserID()
{
return Globals.Decrypt(_userid);
}
public string SetPassword(string pass)
{
Password = Globals.Encrypt(pass);
}
private string GetPassword()
{
return Globals.Decrypt(_password);
}
public override string ToString()
{
return string.Format("user id={0}; password={1}; blah blah blah",
GetUserId(), GetPassword());
}
}

then Globals looks like this:

public class Globals
{
public static byte[] Encrypt(string s)
{
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
return EncryptByteArray(encoder.GetBytes(s));
}

public static string Decrypt(byte[] b)
{
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] result = DecryptByteArray(b);
return encoder.GetString(result);
}

// Encryption keys, fill in with byte values (0-255)
private static byte[] RC2Key = {0x0,0x0,0x0,0x0,0xe0,0x0,0x0,0x0}; //
<==Make up your own
private static byte[] RC2IV = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; //
<==Make up your own

private static byte[] EncryptByteArray(byte[] value)
{
System.Security.Cryptography.RC2CryptoServiceProvi der crypto = new
System.Security.Cryptography.RC2CryptoServiceProvi der();
byte[] buffer;
System.Security.Cryptography.CryptoStream clearTextStream;
System.IO.MemoryStream cypherTextStream;

byte[] result;
buffer = value;

cypherTextStream = new System.IO.MemoryStream();
clearTextStream = new
System.Security.Cryptography.CryptoStream(cypherTe xtStream,
crypto.CreateEncryptor(RC2Key, RC2IV),
System.Security.Cryptography.CryptoStreamMode.Writ e);
clearTextStream.Write(buffer, 0, buffer.Length);
clearTextStream.FlushFinalBlock();
result = cypherTextStream.ToArray();
return result;
}

private static byte[] DecryptByteArray(byte[] value)
{
System.Security.Cryptography.RC2CryptoServiceProvi der crypto = new
System.Security.Cryptography.RC2CryptoServiceProvi der();
byte[] buffer;
System.Security.Cryptography.CryptoStream cypherTextStream;
System.IO.MemoryStream clearTextStream;
byte[] result;

buffer = value;
clearTextStream = new System.IO.MemoryStream();
cypherTextStream = new
System.Security.Cryptography.CryptoStream(clearTex tStream,
crypto.CreateDecryptor(RC2Key, RC2IV),
System.Security.Cryptography.CryptoStreamMode.Writ e);
cypherTextStream.Write(buffer, 0, buffer.Length);
cypherTextStream.FlushFinalBlock();
result = clearTextStream.ToArray();
return result;
}

}
There it is, FWIW.

Brian W
As the risk of being chastized here it wat
"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi:

I'm storing my dB connection in web.config file. Since it will be easily
read by opening file, what is a good way to secure it?

Thanks,
Charlie

Nov 18 '05 #4
See my reply to "Using encrypted dB connection string" in this newsgroup
"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi:

I'm storing my dB connection in web.config file. Since it will be easily
read by opening file, what is a good way to secure it?

Thanks,
Charlie

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tyson Marchuk | last post by:
Hello, Background info Kind of new to using databases and I'm writing an app in C# which connects to a MySQL database. I was using the ODBC connector and a DSN entry to connect to the database...
6
by: Jon Davis | last post by:
I like the drag-and-drop accessibility of dragging a table to a Web Forms designer and seeing a SqlDataAdapter automatically created for me.. being able to create a DataSet from that is fun and...
6
by: Mike L. | last post by:
Hi, Pls, beware that I'm new to this :) I've developed several web appl, either with ASP or ASP.NET. All using SQL server as the back end. In my development environment, I have a single...
19
by: Jaime Stuardo | last post by:
Hi all.. I have created a business logic component that is used from my ASP.NET webform. It works, but connection string to the database is hard coded, as in this method : public DataSet...
14
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review,...
2
Frinavale
by: Frinavale | last post by:
Hello everyone! I'm having a problem securing my connection string. There are a lot of sites out there that explain how to secure a connection string in the Web.config or App.config file;...
2
by: Thorsten Dittmar | last post by:
Hi, I don't get it. I'm using a typed dataset with table adapters and all that stuff. I have the database server running locally on my development system. Now: when creating a tableadapter...
9
by: =?Utf-8?B?QW1tZXI=?= | last post by:
I've read many incomplete opinions about the "Best Practice" for securely accessing SQL but what I really need to find the "Best Practice" that fits my applications needs. Currently (alpha...
2
by: Johnson | last post by:
I'm trying to fix a "sub optimal" situation with respect to connection string management. Your thoughtful responses will be appreciated. I just started with a new client who has a bunch of legacy...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.