/// <summary> /// Performs an action while impersonated under the anonymous user (NT AUTHORITY\ANONYMOUS LOGIN). /// </summary> public static void Run(Action callback) { using (var threadHandle = ThreadHandle.OpenCurrentThreadHandle()) { bool impersonated = false; try { impersonated = ImpersonateAnonymousToken(threadHandle); if (!impersonated) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } callback(); } finally { if (impersonated && !RevertToSelf()) { Environment.FailFast("RevertToSelf() returned false!"); } } } }