views:

2937

answers:

3

Hi,

I want to store the username/password information of my windows service 'logon as' user in the app.config.

So in my Installer, I am trying to grab the username/password from app.config and set the property but I am getting an error when trying to install the service.

It works fine if I hard code the username/password, and fails when I try and access the app.config

public class Blah : Installer
{

    public Blah()
    {

     ServiceProcessInstaller oServiceProcessInstaller = new ServiceProcessInstaller();
                ServiceInstaller oServiceInstaller = new ServiceInstaller();            

                oServiceProcessInstaller.Account = ServiceAccount.User;

     oServiceProcessInstaller.Username =    ConfigurationManager.AppSettings["ServiceProcessUsername"].ToString();

    }
}
+1  A: 

The problem is that when your installer runs, you are still in installation phase and your application hasn't been fully installed. The app.config will only be available when the actual application is run.

You can however do the following:

  1. Prompt the user for the username and password within the installer (or on the command line).
  2. Pass this information to your installer class (google it)
  3. Within your installer class, there is a variable that tells you the installation path
  4. Within the appropriate event in the installer, use System.IO functions to open the app.config file and insert the user entered information
Robert Wagner
A: 

You really shouldn't store a password in an app.config file, that is very bad. You need to either use the service account, the current user or prompt them. Also a user can right click an .exe (which presumably is what is triggering your install) and select "run as" to change their credentials before installation (in which case current user would be a good selection).

Additionally in the services manager a user can change which user the service is supposed to run as after the installation is over. But you definitely don't want to store passwords in plain text files.

justin.m.chase
+2  A: 

Just some ideas on accessing config files inside an installer.

Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath); ConnectionStringsSection csSection = config.ConnectionStrings;

Assembly Path can be gotten several ways: Inside Installer class implementation with: this.Context.Parameters["assemblypath"].ToString(); or sometimes with reflection: Assembly service = Assembly.GetAssembly(typeof(MyInstaller)); string assemblyPath = service.Location;

Johan Botha