public WindowsImpersonationScope(string username, string password, string domain = "") { bool isAuthenticated = NativeFunctions.LogonUser(username, domain, password, NativeFunctions.LOGON32_LOGON_INTERACTIVE, NativeFunctions.LOGON32_PROVIDER_DEFAULT, ref userToken); if (!isAuthenticated) { throw NativeFunctions.GetLastWin32Error(); } impersonationContext = WindowsIdentity.Impersonate(userToken); }