9

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();

    }
}
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

4 Answers4

14

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;
devdigital
  • 34,151
  • 9
  • 98
  • 120
7

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
  • 17,515
  • 9
  • 56
  • 72
2

I had the same problem with a service installer. You have to call your config file "myService.exe.config" and use the OpenExeConfiguration method with the assembly path to look for the right config file (as it is explained in the first answer, when your installers run, the base directory is the directory of the installUtil and not your installer)

{
Assembly __ServiceAssembly = Assembly.GetAssembly(typeof(MyServiceInstaller));
Configuration config = ConfigurationManager.OpenExeConfiguration(__ServiceAssembly.Location);
KeyValueConfigurationCollection  svcSettings = config.AppSettings.Settings;
info("Service name : " + svcSettings["ServiceName"].Value);
}

If you don't want to follow the "myService.exe.config" format, use an exeConfigurationFileMap:

{
Assembly __ServiceAssembly = Assembly.GetAssembly(typeof(SyslogServiceInstaller));
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = 
     Path.Combine(Directory.GetParent(__ServiceAssembly.Location).ToString(),
     "App.config");

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
     configFileMap, ConfigurationUserLevel.None);
KeyValueConfigurationCollection mySettings = config.AppSettings.Settings;

Console.Out.WriteLine(mySettings["ServiceName"].Value);
}
BenC
  • 1,647
  • 18
  • 25
  • I can't access Configuration or ConfigurationManager inside the installer class... Am I supposed to add some reference? – Aditi Mar 18 '13 at 09:30
  • Wrote this a long time ago. Here is my using list: `using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Linq; using System.Configuration; using System.ServiceProcess; using System.Reflection; using System.IO; ` – BenC Jun 06 '13 at 08:05
0

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
  • 13,061
  • 8
  • 52
  • 100