My IIS AppPool runs as a specific domain user, so that my site can access SQL server via Windows authentication. That works well.
But I'm also creating Windows Scheduled tasks dynamically. I can't create these tasks as the IIS App Pool user, as the user is not logged in on the web server and thus the tasks do not run.
With help from this link, I can now create my Scheduled Tasks as the NetworkService user. But when running my app from IIS, it does now quite work so well.
It still creates the tasks, but the task is also trying to log into SQL with Windows Authentication but seems to be using the local System user which does no have Win Auth permissions to SQL.
Are there a way that my Scheduled Task will be able to login to SQL with win auth? (running the task as domain user will not work as the task can only execute if either the user is logged in, or I type in the password which will not be possible).
Or am I completely on the wrong path and I should try something completely different?
My code for creating the task:
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "My Test";
td.Principal.Id = "NT AUTHORITY\\NETWORKSERVICE";
td.Principal.LogonType = TaskLogonType.ServiceAccount;
td.Triggers.Add(new DailyTrigger {DaysInterval = 1});
td.Actions.Add(new ExecAction("c:\\mydir\\myjob.exe"));
td.Settings.AllowDemandStart = task.AllowDemandStart;
td.Settings.Hidden = task.Hidden;
ts.RootFolder.RegisterTaskDefinition("My Test", td,
TaskCreation.CreateOrUpdate, "NT AUTHORITY\\NETWORKSERVICE", null,
TaskLogonType.ServiceAccount);
}