public void ChangeThreadToImpersonate() { ThreadRunner.Run((() => { using (var token = Security.OpenThreadToken(AccessTokenRights.Query, openAsSelf: true)) { token.Should().BeNull(); } using (var token = Security.OpenProcessToken(AccessTokenRights.Duplicate | AccessTokenRights.Query)) { using (var duplicate = Security.DuplicateToken(token, AccessTokenRights.Query | AccessTokenRights.Impersonate, ImpersonationLevel.Impersonation)) { using (ThreadHandle thread = Threads.GetCurrentThread()) { Security.SetThreadToken(thread, duplicate); // We didn't actually change from what the process token is using (var threadToken = Security.OpenThreadToken(AccessTokenRights.Query, openAsSelf: false)) { threadToken.Should().BeNull(); } } } } })); }
public static void ImpersonateAnonymousToken() { using (ThreadHandle thread = Threads.GetCurrentThread()) { Error.ThrowLastErrorIfFalse(Imports.ImpersonateAnonymousToken(thread)); } }