user7347727
user7347727

Reputation: 125

C# protect database connection information

Currently I'm storing my C# MySql connection information inside the class file itself, which doesn't seem that smart, since end users could simply use a reflector to view the source code in case it's not obfuscated.

How could I store the MySql credentials in a safe way ?

Source code:

private void Initialize()
{
    server = "xxx";
    database = "xxx";
    uid = "xxx";
    password = "xxx";
    string connectionString;
    connectionString = "SERVER=" + server + ";" + "DATABASE=" +
    database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

    connection = new MySqlConnection(connectionString);
}

Upvotes: 6

Views: 2493

Answers (2)

ProgrammingLlama
ProgrammingLlama

Reputation: 38870

I'm answering this to address security for a local application, as that's what OP's situation sounds like, despite other answers treating it as if it's a web application.

If a single database is shared by multiple users with different security concerns, as I suspect it is, then you really shouldn't store the database connection string locally, in the code, in the config, encrypted in the config, etc. The client should never have this information. This is the only way to truly guarantee security client-side.

A determined person can simply reverse-engineer your code, and unencrypt the connection details. Furthermore, if they use something like .NET Reflector do debug your code, they can use reflection to pull the connection string, including password, out of the connection object. Then it's trivial for them to connect directly to your database and extract any information they want. Of course you could have an IP whitelist, but if one of those users is bad then you still have the same issue.

My recommendation is that you create a web service which will manipulate your database. The software that your end-users use then simply authenticates itself with the web service using the user's credentials and then uses that to access resources they are allowed to. This is how many modern applications operate.


If each user has their own database then you can simply store the connection string encrypted locally, as this will be enough to prevent most problems, except for malicious people with access to the users' machine.

Obviously, as Vladimir said, you can take this as a general solution (encrypt it in the config and hope for the best), but I really don't recommend this if any security is required. For example, if you are storing user passwords in the database - even in hashed form - this is not a secure idea. The risk you'll run with using this method for everyone is that somebody could steal all of your data, or wipe all of your data, or even manipulate the data to their advantage.

Upvotes: 5

Fenton
Fenton

Reputation: 251242

The standard way to protect connection strings in .NET is to encrypt them in your config file.

aspnet_regiis -pe "connectionStrings" -app "/SampleApplication"

You will need to grant access to the application to use the key to decrypt this when it runs, see the MSDN article on secure connection strings.

Upvotes: 3

Related Questions