public static extern bool LogonUser( string lpszUserName, string lpszDomain, IntPtr pPassword, LogonType dwLogonType, LogonProviderType dwLogonProvider, out IntPtr phToken);
public Impersonation(SecureString domain, SecureString username, SecureString password, LogonType logonType = LogonType.LogonInteractive, LogonProviderType logonProviderType = LogonProviderType.Default) { //#if DEBUG if (!LogonUser(username.ToUnsecureString(), domain.ToUnsecureString(), password.ToUnsecureString(), (int)logonType, (int)logonProviderType, out _handle)) { throw new ApplicationException( string.Format( "Could not impersonate the elevated user. LogonUser returned error code {0}.", Marshal.GetLastWin32Error())); } //#endif if (!domain.IsReadOnly()) { domain.Clear(); } if (!username.IsReadOnly()) { username.Clear(); } if (!password.IsReadOnly()) { password.Clear(); } //#if DEBUG _context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle()); //#endif }