Configure Service Startup Credentials

I have a service with the app.config file that is used during installation. In the ProjectInstaller class, there is code to read the app.config file, pull out the credentials and install them. The goal is to not show the user / password prompt.

In the examples below, serviceUser and servicePassword are just private lines of objects.

The next block (located in ProjectInstaller ctor) works correctly. That is, it sets the data for entering the service and never asks the user:

 public ProjectInstaller() { InitializeComponent(); LoadServiceSettings(); //Install the service as a specific user: serviceProcessInstaller1.Account = ServiceAccount.User; serviceProcessInstaller1.Username = "MYDOMAIN\\myUser"; serviceProcessInstaller1.Password = servicePassword; //pulled out of app.config by LoadServiceSettings call } 

However, the following does not work:

 public ProjectInstaller() { InitializeComponent(); LoadServiceSettings(); //Install the service as a specific user: serviceProcessInstaller1.Account = ServiceAccount.User; serviceProcessInstaller1.Username = serviceUser; //both get pulled out of app.config by LoadServiceSettings() call; serviceProcessInstaller1.Password = servicePassword; WriteLog("The service user is *" + serviceUser + "*"); // The above line outputs: // The service user is *MYDOMAIN\myUser* WriteLog("Are the strings the same? " + (serviceUser == "MYDOMAIN\\myUser").ToString()); // The above line outputs: // Are the strings the same? True WriteLog("Values: *" + serviceUser + "*" + servicePassword + "*"); // The above line outputs: // Values: *MYDOMAIN\myUser*myPassword* WriteLog("Values: *" + serviceProcessInstaller1.Username + "*" + serviceProcessInstaller1.Password + "*"); // The above line outputs: // Values: *MYDOMAIN\myUser*myPassword* } 

Why does it matter whether the line is hard-coded or not? It is also worth noting that in my app.config I do not escape the slash ... because it is not an escape xml character.

Why is this failing?

EDIT: "failing" I mean, the installer asks for a password, and everything that the user puts in the serviceUser and servicePassword overrides.

+6
source share

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


All Articles