Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi
I have a theoretical question regarding the installation of a windows service under user account. When you think about it, it doesn't seem ok that the windows service would run under a user account, since a service can be started before any user logs in. Is it even possible to install / configure the service to run under user account? When I try to install a service with a ServiceProcessInstaller.Account property set to ServiceAcount.User, I get a prompt to enter the user credentials(even though they were set in the OnBeforeInstall method via the Username and Password properties of the ServiceProcessInstller) and when I enter them the installation still failes, saying that the provided credentials are not valid. If it is not possible for a service to run under the user account what does setting the ServiceProcessInstaller.Account to ServiceAccount.User then do and when would one use the Password and Username properties?

Uros
Posted

Yes, this is possible. (However, I wonder why? It's better be avoided. However, I understand that is some situations you may need it.)

First, please see my past answer with very brief overview of installation: How to install windows service programmatically[^].

Basically, to make you service installable you need to implement two classes based on System.ServiceProcess.ServiceProcessInstaller and System.ServiceProcess.ServiceInstaller, please see:

http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller.aspx[^],
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx[^].

In both of the two MDSN help pages referenced above, you will find code samples showing how to write the implementation of the installer.

Now, look at the class System.ServiceProcess.ServiceProcessInstaller and pay attention for its properties: Account, Username and Password. This is how you can customize the account.

In your case, you should use System.ServiceProcess.User for the Account property value, please see:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx[^].

Note that the solutions with the account and password is the inherent security problem. Hard-coding of the account and password in the code is potentially unsafe. You could devise some secure mechanism for obtaining the password just once and removing it, but the installation itself will result in storing the password in the system. It's hard to give you an ultimate advice for solution of this problem, but this is just something you should understand.

I would simply create some account with insecure password and limit the permissions of these account to minimum barely required just to run the service. In particular, deny access to any disk volumes except one volume and one directory (or so) only need to run the service.

—SA
 
Share this answer
 
v3
Comments
koleraba 13-Feb-12 8:17am    
Thank you for your reply. Maybe you didn't understand my question correctly. I know how to install a windows service and how to use ServiceInstaller and ServiceProcessInstaller. Everything works ok as longs as the service is not using a user account. When installing under user account the service fails to install and gives me an error that the supplied credentials are not valid.

Uros
Thank you for your time, but as it turns out the problem lied elsewhere. I am using the InstallHelper to programmatically install the service. In the call to the InstallHelper I pass username and password as command line arguments. It seems that the order of the arguments is important. If you specify the path to the .dll before other arguments, those arguments are not passed to the installer, and the installation failes.
 
Share this answer
 
Comments
PetrosPetrosean 3-May-16 7:36am    
Is it possible to dispay the source code of InstallHelper?
Thank you very much in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900