views:

371

answers:

3

Hi,

I have a windows service that runs fine, but I have to have it run under a special user account.

Currently I go into services and change the logon as section, but for deployment this has to be done more professionally.

Is there a way for me to have it logon as a custom user account programatically, or during the installation process of the service?

+3  A: 

When you open the Service COntrol Manager,(SCM), of course,there is a tab labeled Logon.. In there you can specify which domain or machine account it should run under...

But programatically. if you use a Service Installer class in your code you can specify it there..

 public class MyServiceInstaller : Installer
    {
        private ServiceInstaller servInst;
        private ServiceProcessInstaller servProcInst;
        public MyServiceInstaller () { InitializeComponent(); }

        #region Component Designer generated code
        private void InitializeComponent()
        {
            servInst = new ServiceInstaller();
            servProcInst = new ServiceProcessInstaller();
            // -----------------------------------------------
            servProcInst.Account = ServiceAccount.LocalSystem; // or whatever accnt you want
            servProcInst.Username = null;  // or, specify a specifc acct here
            servProcInst.Password = null;
            servProcInst.AfterInstall += 
                new InstallEventHandler(this.AfterServProcInstall);
            servInst.ServiceName = "MyService";
            servInst.DisplayName = "Display name for MyService";
            servInst.Description = " Description for my service";
            servInst.StartType = ServiceStartMode.Automatic;
            servInst.AfterInstall += 
               new InstallEventHandler(this.AfterServiceInstall);
            Installers.AddRange(new Installer[] { servProcInst, servInst });
        }
        #endregion
    }
    private void AfterServiceInstall(object sender, InstallEventArgs e) { }
    private void AfterServProcInstall(object sender, InstallEventArgs e) { }
Charles Bretana
Do I need those AfterInstall delegates?
Blankman
Not exactly sure... Try it w/o... this code is from severla years ago, so I don;t remember why, but in mycode, they are just empty delegates... (Added to answer...)
Charles Bretana
thanks. btw, i'm getting errors trying to fetch the username/password from app.config, guess I can't store user/pwd there??
Blankman
Yes, you can, you can store anything in app.config.. Sounds like another question...
Charles Bretana
A: 

How are you installing? Is this a .net service (in which case, I think you can specify the account on the installer object).

Generally the installer technology allows you to change the credentials (with the possible exception of COM's service registration)

If you're doing an xcopy registration and then registering on the first run, you can use ChangeServiceConfig(..., ServiceName, Password,...) to fix up the registration.

James Ogden
A: 

Take a look at the CreateService function, particularly at the lpServiceStartName argument. That's the "name of the account under which the service should run."

Arnout