mattjohnsonpint/SimpleImpersonation

Not an issue but a question: How to impersonate Local System user

danielh1989 opened this issue · 1 comments

I try to impersonate the LocalSystem user which is user for Windows Services.
I tried the following code:

using (var userHandle = UserCredentials.LocalSystem.LogonUser(LogonType.NewCredentials)) { WindowsIdentity.RunImpersonated(userHandle, () => { File.WriteAllText("Test.txt", "TEST"); }); }

But the file is always created as Owner "Administrators". My user-account, running this code, is part of the administrators-group, but I expect the Owner of the file to be "SYSTEM".
Which "LogonType" do I need to impersonate the LocalSystem account?

The only way to impersonate the LocalSystem is to be running within a process that is already launched with LocalSystem as the owner. Anything less would create a security vulnerability.

This has come up before. See #8 and linked article therein. Thanks.