Saving the connection string in the app.settings but..

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
..the wizard offers 2 options:

1 = to save the whole string, password and all, in the settings
2 = to save only data that doesnt compromise security



so i'm really wondering, if im using datasets and tableadapters that know how and where to get their own connection strings from... how can i make it so that when it gets a connection string, it gets the password along with it?

to clarify my question: i have a doubt that if i do not store the password in the string,a nd then go and make a new MyTableAdapter - it will go and get a connection using the connection string it finds in app.settings - i.e. a COnn string with no password. How will it then know where to look for the password? or, how can i tell it in code, what the password will be, so that it uses this password every time?
 
If you don't store the password as part of the connection string then you need to store it elsewhere, presumable encrypted, or else have the user provide each time they use the app. .NET 2.0 supports encryption of all or part of a config file, so you could save the password with the connection string and encrypt the whole connection string or else store the connection string unencrypted and store the password elsewhere, either in the config file or somewhere else, in an encrypted state. You can then use an SqlConnectionStringBuilder to insert the password:
VB.NET:
Dim myConnectionStringBuilder As New SqlConnectionStringBuilder(myConnection.ConnectionString)

myConnectionStringBuilder.Password = "password from encrypted source"

myConnection.ConnectionString = myConnectionStringBuilder.ConnectionString
If you want more information on encrypting config files then search MSDN for "encrypt config" (without quotes). Note that most of the information given is geared towards ASP.NET but the same principles apply to WinForms. You just have to use the -pef switch when invkoing aspnet_regiis. The sections on Web Farm Scenarios are relevant to distributing apps to end user machines.
 
Back
Top