When using Entity Framework to access a database on a non-local server how should I be specifying the username and password parameters in the connection string (which is stored in the web.config file)?
I've read in a C# step by step guide(2010 edition by John Sharp) to never hard code them into your application due to potential reverse engineering or if someone gets hold of the source code. So I would like to know the conventional best practice for doing so.
Hard coding the user and password is bad for 3 reasons:
There is no magic solution to this problem and security in this case is only as good as the discipline and good will of the people in charge with the security.
In my company, it goes something like this:
web.config
comming from the development department with their own secrets when deploying the application on the production machinesEncrypting the user and password in the web.config
can only help you so much. Eventually you'll have to hard code the encryption key, in clear form, in your application and that takes us back to the disassembling problem.
In my opinion, a very good solution would be a combination of what's going on in my company and encryption with a clear key and obfuscation.
The general idea is:
That means that someone (maybe the owner of the company or some other head) needs to use a "greasemonkey" app to encrypt usernames and passwords and give the resulting encryptions to the application administrators.
Don't forget there's also the db administrators which initially gave the owner an initial pair of credentials. The owner needs to change the password and then do everything I laid out.
In conclusion, there are many solutions, some wackier than others. It's not all in the tools and code but also in the discipline.
You can encrypt sections of your web.config. See this walkthrough on MSDN: http://msdn.microsoft.com/library/dtkwfdky.aspx It's pretty simple to follow.
You can encrypt a particular section of a web.config file easily using the method shown in this document: Encrypting and Decrypting Configuration Sections
It is transparent for your application code and the encrypted section is useless outside the machine it was encrypted on.
You can try with following code,
ConnectionStringsSection oSection = Configuration.ServiceConfiguration.GetConnectionStrings();
if(!oSection.SectionInformation.IsLocked && !oSection.SectionInformation.IsProtected)
{
oSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
oSection.CurrentConfiguration.Save();
}
EDIT:
you can get more information on Protected configuration from MSDN Link, http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With