cannot creating a task without running app as admin

Topics: Errors
May 7, 2015 at 4:57 AM
when trying to create tasks I get access denied unless the program is stated as admin. the user is a local admin on the system, the task is being created on the same system, and the task is to be run as a different user. below is the gist of the code, please let me know
using (TaskService ts = new TaskService())
{
           TimeTrigger start = new TimeTrigger { StartBoundary = dateTime1.Value };
            TaskDefinition td = ts.NewTask();
            td.Triggers.Add(start);
            td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Settings.Compatibility = TaskCompatibility.V2_2;
            td.Actions.Add(new ExecAction(Environment.GetCommandLineArgs()[0], cmd, null));
            ts.RootFolder.RegisterTaskDefinition("Task Name", td, TaskCreation.Create, username, password, TaskLogonType.Password);
}
any ideas?
Coordinator
May 10, 2015 at 7:29 PM
The code calling the TaskService constructor must be running as Administrator to avoid UAC blocks. The security rights of the TaskService instance will determine if the RegisterTaskDefinition method can be called. Registering a task as a specific user instruct that task to run as that user.
Marked as answer by dahall on 9/1/2015 at 10:00 AM