This project has moved and is read-only. For the latest updates, please go here.

RegisterTaskDefinition with Windows Service credentials

Topics: Examples
May 16, 2014 at 1:23 PM
I'm currently scheduling tasks using SYSTEM account:
  folder.RegisterTaskDefinition(, td, TaskCreation.CreateOrUpdate,
     "SYSTEM", null, TaskLogonType.ServiceAccount);
Scheduling is done from a Windows Service, that is configured by default with SYSTEM account. Since windows service's user credentials can be modified, I would like to schedule tasks using the user who is currently running the service, instead of always using SYSTEM.

Is it possible to call RegisterTaskDefinition with the current service's user and password?
Thank you, Valentina
May 17, 2014 at 6:11 PM
You can supply any user credentials you want. Credentials supplied through the TaskService constructor determine access to the host specified to work with the Task Scheduler and then the credentials supplied to the RegisterTaskDefinition method determine the run time access of the task.
May 18, 2014 at 9:04 AM
Hello dahall,
thank you for your reply.
I was thinking of something "automatic": since I don't know the username and the password of Windows Service's user (and the password isn't stored in plain text, anyway) if I don't specify any credential on RegisterTaskDefinition, which user is then used by the Windows Task Scheduler?
Thanks again, bye!
May 18, 2014 at 4:15 PM
Ah, I think I see. You are looking at impersonating the current user and then scheduling tasks. Others have tried this with mixed results. According to the Microsoft documentation, Task Scheduler should run using the current security token if the TaskService is instantiated with no user credentials. However, in practice, this doesn't always work. From the library's standpoint, if you are able to instantiate TaskService in the context of the current user, you then will call RegisterTaskDefinition without user credentials as well in order to register the task in their security context.
Marked as answer by dahall on 9/1/2015 at 10:04 AM
Jun 4, 2014 at 1:20 PM
Hello dahall,
thanks, you're always very kind.
I solved it by asking the user for username and password and using the RegisterTaskDefinition method with the credentials specified.
Have a nice day, Valentina
Marked as answer by dahall on 9/1/2015 at 10:04 AM