I’m currently working on a ASP.NET MVC web site, and I’ve come up to a point where I need to integrate a database into the website.
Normally I would simply add the appropriate connection string to the Web.config file:
<add name="MainDB"
connectionString="Server=localhost; Database=TopSecretData; User Id=Joe;
password=password" providerName="System.Data.SqlClient" />
But there’s obviously a glaring security flaw if I leave my User Id and password right in the Web.config, especially when it’s under source control.
In short: How can I store my connection string details without having it publicly visible?
Best practice is to encrypt your connection strings section. Use aspnet_regiis.exe, which can be found in various places:
Before:
Run this command:
Or, if the above command doesn’t work (and you get the aspnet_regiis help text), try
where the “6” is the ID of the site as reported in IIS.
After:
Now that it is garbled, you can’t edit it.
Decrypt like this:
Or
And then change and re-encrypt.
To read the connection string, use the ConfigurationManager static class.