views:

1219

answers:

5

What is the best way to manage connection strings in a web application, from a security standpoint? I've done it several different ways. I've stored them as plain text web.config setting keys. I've also created a "Constants" class that has public read-only string properties for each connection string.

Does anybody have any recommendations for managing connections strings in such a way that I will have no concern about them being maliciously discovered? I am definitely open for encryption.

+7  A: 

Storing the encrypted connection string in web.config file is a good practice.

Gulzar
Since I've not done this before, please provide a relevant resource.
Josh Stodola
+3  A: 

you can encrypt your connection strings in your web.config file.

storing connection strings in a class as a property or a constant is not secure. anyone who uses a disassembler can see your connection string.

best way is the configuration encrypting.

Canavar
You might also want to use SecureString rather than a regular string so a memory dump wont expose it either...if you're that way inclined.
Stimul8d
+2  A: 

You can encrypt and decrypt sections of your web.config by using the command line tool aspnet_regiis:

Encrypt: aspnet_regiis -pef "connectionStrings" "c:\folder\"

Decrypt: aspnet_regiis -pdf "connectionStrings" "c:\folder\"

Jakob Christensen
+1  A: 

@vartec: It's not quite a SNAFU..

IIS can indeed read encrypted text if you encrypt it using standard .NET encryption mechanism that won't break any UTF8 or Unicode encoding. Microsoft is also encouraging this as a best practice.

You can see a sample on encrypting connection string from this:

"How to: Secure Connection Strings When Using Data Source Controls"

http://msdn.microsoft.com/en-us/library/dx0f3cf2.aspx

eriawan
+2  A: 

If you have full control over the server you can also store the connection string in your Machine.Config. This can be handy if you have lots of applications which all work with the same DB server.

I'm not sure if its worth encrypting it since you have to access to the server in the first place to view the machine.config. And if your server's been compromised the Encyrption won't stop a hacker from pulling the credentials from the config file.

JoshBerke
It does protect you from accidentally exposing the contents of web.config, as long as an attacker can't upload their own .aspx file into your application directory.
finnw
A similar caveat applies to hashing user passwords - it protects you only from leaked database backups. Both are useless if an attacker gets control of the webserver - but they are worth doing anyway.
finnw
We do this where I work so for our different servers (dev, test, prod) so the appropriate connection string is always used for each. Works very well.
spilliton
Once the connection strings are in your machine.config you better not be exposing it. If you are your already toast. Encrypting in a web.config is different. You tend to archive the web.config files and what not so it can help keep internal prying eyes from seeing them
JoshBerke