dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.2k stars 191 forks source link

Network drive credentials not working for app that autostarts with scheduled task #901

Closed Taxxor90 closed 2 years ago

Taxxor90 commented 2 years ago

Hi, maybe this isn't exactly related to this project but the windows task scheduler itself, however I'll try to explain the problem and maybe there's a solution here.

I have an application that runs as administrator and that I made an autostart task for. This application has a directory observer that accesses a network drive. The credentials for this network drive are saved in windows so everytime I open the app, it can access it right away without problems.

This is the setup for the task:

                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = "Autostart";
                    td.Settings.DisallowStartIfOnBatteries = false;
                    td.Settings.StopIfGoingOnBatteries = false;
                    td.Principal.RunLevel = TaskRunLevel.Highest;

                    var trigger = new LogonTrigger();
                    trigger.UserId = Environment.UserName;

                    td.Triggers.Add(trigger);
                    td.Actions.Add(new ExecAction(appPath));
                    ts.RootFolder.RegisterTaskDefinition(appName, td);

The problem is every time I log on and the app autostarts, it states that it couldn't open the directory. When I select the network drive within the app, I have to type in its credentials again(it says thet they were wrong in that window) and then it's working until the next logout/login.

However, if I don't type them in and instead just close the app and start it again(even if I go into task scheduler and start it through the context menu there) it starts and has the directory working. It also hasn't anything to do with it being launched too early, even if I set a delay of 30s the directory isn't working when the app is started by the trigger. If I manually fire the task earlier, it works. The user that is set in the triggers security options is the user I use to log in,

What could possibly happen here that it's not working as expected? What difference does it make weather I trigger the task manually inside the task scheduler or if the task get's triggered automatically on logon?

I've used a simple registry entry for the auto start before and that was working fine too, the reason I switched to scheduled task was that the UAC dialog was bothering people who had that active.

Taxxor90 commented 2 years ago

Addition: I just thought I'd be clever by creating a batch file that starts the app and then let that file be run as admin by the task scheduler, seeing that it worked when I manually started the app after log on... But it also shows the same behaviour while manually executing the batch file again works fine....

dahall commented 2 years ago

Two thoughts, each requiring some testing: 1) When creating the ExecAction, pass in the working directory as the third parameter to the constructor. This may solve the directory problem. 2) When calling RegisterTaskDefinition without any parameters, you are defaulting to the current user using S4U. You may wish to try calling the method with all the parameters and with InteractiveToken:

ts.RootFolder.RegisterTaskDefinition(appName, td, TaskCreation.CreateOrUpdate,
   Environment.UserDomainName + "\\" + Environment.UserName, null, TaskLogonType.InteractiveToken);
Taxxor90 commented 2 years ago

Thanks for the quick response.

Unfortunately, the problem still persists, this is how the code looks now:

ts

I've also tried the use of InteractiveToken before but reverted back as it didn't help. I'm using Windows 11 btw, don't know if that could have something to do with it.

Taxxor90 commented 2 years ago

Wow, I think I might have found a solution, although it's maybe not something every user would like to do... The network drive isn't showing up as a normal drive in the folder picker when choosing the directory, but that never bothered me because I could just select "Network" on the left side and select my NAS from there(where the auto started app would ask for the credentials). But it seems like that can cause problems when the app is running as admin, because the admin can't see the mapped drive of another user(I don't know why it would then only have problems with the scheduled task but work when I manually start it although then it also runs as admin, also can't see it directly and has to be selected through "Network", but without it asking for credentials).

So I searched for a way to have the drive show up as a normal one in the file picker and found this

1 - run regedit

    • locate HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System 3 - create a new DWORD entry with the name EnableLinkedConnections and value 1.

Did that and now the directory is found when opening the app with the autostart task.

Now as I understand it, this would also lead to every user of a PC being able to see(and access without credentials??) every mapped network drive, which isn't quite ideal I think, but at least it's something that seems to work.