[DllImport ("advapi32.dll")] public static extern int LogonUser (String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
private bool impersonateValidUser (String userName, String domain, String password) { TempWindowsIdentity WindowsIdentity; IntPtr token = IntPtr.Zero; IntPtr = IntPtr.Zero tokenDuplicate; if (RevertToSelf ()) { if (LogonUser (userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token)! = 0) { if (DuplicateToken (token, 2, ref tokenDuplicate)! = 0) { tempWindowsIdentity = new WindowsIdentity (tokenDuplicate); impersonationContext tempWindowsIdentity.Impersonate = (); if (impersonationContext! = null) { CloseHandle (token); CloseHandle (tokenDuplicate); return true; } } } } if (token! = IntPtr.Zero) CloseHandle (token); if (tokenDuplicate! = IntPtr.Zero) CloseHandle (tokenDuplicate); return false; } private void undoImpersonation () { impersonationContext.Undo (); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)