Where to save the SQL Server connection string in the client console application?

I am creating a Windows console application to monitor user traffic. When a user logs on to his computer (connected to the network), the Windows login time should be updated in the SQL Server database.

All client computers connect through the network.

Question: where can I save the database connection string to the server? If I save the connection string (encrypted) in the console application running in the client application, this will create a problem if after a few months the server name / username / password changes.

Please advise the best approach to this problem.

+4
source share
2 answers

You really can save it in the application configuration file (see @NoOne answer for more details). However, remember that you probably should not store your username and password here.

You have the following options:

(a) encrypt the connectionStrings configuration section. This has some administrative β€œproblems” since you discovered YMMV.

(b) do not save the username and password, but request them from the user at run time; optionally allowing them to be specified as command line parameters, although it has its own problems: the parameters (and therefore the username / password) will be visible using viewing tools such as Task Manager or Process Explorer.

(c) check if you can change the connection credentials from the username / password to integrated security (you do not need to specify the username / password in the connection string and therefore not in the configuration file).

Option (c) is the best. This eliminates the need for a username (and, more importantly) a password in general. However, your application (and environment) should be prepared for this (more details here ).

If you select option (b), you can specify the connection string in the configuration without the User and Password keys. You would allow the user to specify them and then create a real connection string for future use using the SqlConnectionStringBuilder (or DbConnectionStringBuilder ) class:

  var builder = new SqlConnectionStringBuilder( ConnectionManager.ConnectionStrings["test"].ConnectionString); builder.UserID = /* Username specified by user */ builder.Password = /* Password specified by user */ string realConnectionString = builder.ConnectionString; 
+4
source

you must add the app.config file to your project and save the connection string in it.

 Project-->Add->New Item-->Application Configuration File 

Add connection string as

 <connectionStrings> <add connectionString="test" name="test"/> </connectionStrings> 

read it as

 string connectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString; 

But what about the password stored in the connetion string? Infact your entire connection string must be encrypted and given that MS has provided a built-in solution for this. You need to look at the Protection section .

Note: You will need to add a link to System.Configuration in your project

+3
source

Source: https://habr.com/ru/post/1498950/


All Articles