Norbert Forgacs
Norbert Forgacs

Reputation: 593

C# SQL connection string best practice

I have a winforms application that connects to a database with a connection string and a generic user

"Database=DBADAS;Server=TMT123\\SQLEXPRESS;User ID=user; Password=*****;

After connecting into the database with a login dialog, we check if the user and password are existent in the user table from the database.

My question is now if this is a good practice? because basically in the connection string there is every information needed to crack the server.

Upvotes: 2

Views: 7675

Answers (2)

Muhammad Ramzan
Muhammad Ramzan

Reputation: 342

Encrypting Web.Config

  1. Open Command Prompt with Administrator privileges
  2. At the CommandPrompt, enter:

    cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
    
  3. In case your web Config is located in "D:\Articles\EncryptWebConfig" directory path, then enter the following to encrypt the ConnectionString:

    ASPNET_REGIIS -pef "connectionStrings" "D:\Articles\EncryptWebConfig" 
    

Use Aspnet_regiis.exe tool with the –pef option and specify the application path as shown above.

Note: The parameter "connectionStrings" is case sensitive.

For Winforms: You need to rename your app.config to web.config encrypt it by using steps 1 to 3 and again rename it to app.config.

Reference: https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config

Upvotes: 2

CDove
CDove

Reputation: 1950

There are a few ways to go about this safely. Since it's a Winforms app and not a web application, most of your security risks involve someone already in your network peeking at the connection string. This adds a layer of security in and of itself.

1. Hardcoded You can hardcode the connection string that fetches the users into a DLL and make your application depend on that DLL. I only suggest this because it sounds like the "generic user" password is permanent; otherwise, you'd have to recompile code every time you changed the password, then deploy again. This is very secure, however, as the value isn't stored in plain text.

2. App.Config You can stick it in a configuration file. Within a secured network, this is probably the most versatile option, as you can store multiple strings and easily update them without updating the full application. This goes well alongside settings like a "DebugMode" setting, etc. Using App.Config or another XML file is ideal, but you can roll a quick and dirty .txt file, too.

3. Database Probably the most secure way of all, as you can encrypt your database and code your programs to fetch their connection strings and login information from that database by using an unrelated login. This allows greater control over what can be reached by the application when a user has not yet logged in. It also prevents the software from operating outside of the network, which may be desirable.

4. Internal API Having a separate application serve this data divorces user capability from your concerns, as the API and your app can exchange verification keys to see if your app even has permission to try to connect. This is my personal favorite, but obviously the most work to set up.

5. Registry Entry Depending on how you have this installed, it may work well to embed the tokens you need in the Registry. This guarantees the app requires admin permissions to install, and allows you to use Windows security to restrict access to the hive.

Again, since it's an internal non-web app, I wouldn't worry too much about the plain text of the connectionstring; if someone has gotten this far into your network, chances are you have much bigger problems already. I wouldn't leave it floating as a plain text file in a local directory, but any degree of security above that is probably acceptable for your purposes.

Upvotes: 5

Related Questions