Install Windows Service with Custom User Credentials

Install Windows Service with Custom User Credentials

am 04.12.2007 18:01:01 von B.Ahlstedt

Hi all,
This is something that I have been toying with for about a week now. What
I want to achieve is Install a Service with Customised parameters (using
InstallUtil.exe) for User Name. Example (C#);

[RunInstaller(true)]
public class MyServiceInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller;
private System.ServiceProcess.ServiceInstaller serviceInstaller;

public MyServiceInstaller()
{
this.serviceProcessInstaller = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();

// Null out UserName and password
//POINT1
this.serviceProcessInstaller.Password = null;
this.serviceProcessInstaller.Username = null;
// this.serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.User;

// Service Installer, set ServiceName to same as ServiceName in
ServiceBase derived class
this.serviceInstaller.ServiceName = "MyService";

// Add Both Installers to Project Installers Collection to be Run
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller,
this.serviceInstaller});
}

//POINT2 Override Install Method to add customised values
public override void Install(System.Collections.IDictionary stateSaver)
{
Console.WriteLine("Install Start...");

// Check for Custom User
if (this.Context.Parameters.ContainsKey("MyUserName") == true)
{
Console.WriteLine("******* Using CUSTOMISED values ....");
this.serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.User;
this.serviceProcessInstaller.Password =
this.Context.Parameters["MyPassword"];
string user = this.Context.Parameters["MyUserName"];
string domain = this.Context.Parameters["MyDomainName"];

// Do we use Principal Name @ Authority or
Authority\Principal ( Domain\User )
string userName = string.Empty;
if (domain.IndexOf(".") > -1 && domain.Length > 1)
{
userName = string.Concat(user, "@", domain);
}
else
{
userName = string.Concat(domain, "\\", user);
}
// Update the Correct user Name
this.serviceProcessInstaller.Username = userName;
}
else
{
Console.WriteLine("******* Using HARDCODED values ....");
this.serviceProcessInstaller.Password = "password";
this.serviceProcessInstaller.Username = "DOMAIN\\User";
this.serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.User;
}

Console.WriteLine("******* Username: {0}, Account: {1},
Password: {2}",
this.serviceProcessInstaller.Username,
this.serviceProcessInstaller.Account,
this.serviceProcessInstaller.Password);

// Call the Base Class to finish Installation
base.Install(stateSaver);

Console.WriteLine("Install End...");
}
}

POINT1 - I have nulled the User Name and Password Fields and left the
Account Type as default in the MyServiceInstaller Constructor. I could
hard-code the values here, but that defeats the purpose of the exercise.

POINT2 -
a) Running the command "InstallUtil MyService.exe" generates the following
Console Log (minus .NET. Framework Installation ...);
Install Start...
******* Using HARDCODED values ....
******* Username: DOMAIN\User, Account: User, Password: password
Install End...
b) Running the command "InstallUtil /MyUserName=User /MyDomainName=DOMAIN
/MyPassword=password MyService.exe" generates; (after Uninstall if above was
run first "InstallUtil /u MyService.exe")
Install Start...
******* Using CUSTOMISED values ....
******* Username: DOMAIN\User, Account: User, Password: password
Then Error Details;
An exception occurred during the Install phase.
System.ComponentModel.Win32Exception: The account name is invalid or does
not exist, or the password is invalid for the account name specified
at System.ServiceProcess.ServiceInstaller.Install(IDictionary stateSaver)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at TemplateService.TemplateServiceInstaller.Install(IDictionary
stateSaver) in
D:\Projects\Test\Services\TemplateService\TemplateServiceIns taller.cs:line 124
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.AssemblyInstaller.Install(IDict ionary
savedState)
at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
at System.Configuration.Install.TransactedInstaller.Install(IDi ctionary
savedState)

The Rollback phase of the installation is beginning.
See the contents of the log file for the
D:\Projects\Test\Services\TemplateService\bin\debug\Template Service.exe
assembly's progress.
The file is located at
D:\Projects\Test\Services\TemplateService\bin\debug\Template Service.InstallLog.

The Rollback phase completed successfully.

The transacted install has completed.

The machines that this occurs on are Windows XP Pro and XP Pro 64 (with
their respective x86 and x64 InstallUtil versions) and using .NET Framework
2.0, both connected to a Domain Controller. Changing the User Credentials to
a Local User, Domain Administrator still offers no difference, Hard coded
within Installer works, but using passed custom values to the Install Utility
seems to create some corruption on the install process.

This worked with InstallUtil in .NET v1.1 so am at a loss as to why
Hardcoding works but using the Context.Parameters StringDictionary creates
some kind of Error that InstallUtil cannot handle.

Anyone able to shed some light on this?

TIA Bjorn Ahlstedt

RE: Install Windows Service with Custom User Credentials

am 05.12.2007 12:31:01 von BAhlstedt

In addition, if I comment out the whole Install override, the InstallUtil.exe
utility will provide the "Set Service Login" popup, and placing the *exact*
same DOMAIN\User and password combination as is Hard Coded or through the
Parameters, the installation completes successfully. This leads me to believe
that the problem lies with InstallUtil.exe and custom parameter handling.
Again this worked in the .NET v1.1 of the Framework but not on .NET v2.0 (x86
and x64).

Since the problem with InstallUtil.exe, I decided to test further, so I
added a Setup Project (right-click Solution -> Add... new Project -> Other
Project Types ... Setup and Deployment -> Setup Project) for my service. See
link (http://support.microsoft.com/kb/816169) for the general idea.

To work with my Installer and Install Override method, I had to add the
following to the Custom Actions -> Install -> Primary output ->
CustomActionData property;
"/MyUserName=[MYUSERNAME] /MyDomainName=[MYDOMAINNAME]
/MyPassword=[MYPASSWORD]".

Also I modified the Install Method slightly to handle better,
from:
public override void Install(System.Collections.IDictionary stateSaver)
{
Console.WriteLine("Install Start...");

// Check for Custom User
if (this.Context.Parameters.ContainsKey("MyUserName") == true)

to:
public override void Install(System.Collections.IDictionary stateSaver)
{
Console.WriteLine("Install Start...");

// Get the User name
string user = this.Context.Parameters["myUserName"];

// Check for Custom User
if (user != null && user.Length > 0)

Then installing using the command line;
'setupMSIname.msi' - installs fine - uses hardcoded values.
or
'setupMSIname.msi MyUserName="{user}" MyDomainName="{domain}"
MyPassword="{password}"' - where values in between { and } are the
appropriate values, also installs fine, but with the Custom Parameterised
values.

Is there any reason why the InstallUtil.exe doesn't accept the user
credentials when passed as parameters from a batch file/command line, except
when run from a MSI Setup (if it does internally use the InstallUtil.exe)??

Hopefully I am missing something completely in which I can then humbly
accept and fix so that my InstallUtil.exe implementation works as required.

TIA