private WindowsIdentity Logon() { try { const int LOGON32_LOGON_NETWORK = 3; const int LOGON32_PROVIDER_DEFAULT = 0; // attempt to authenticate domain user account bool logonSucceeded = Shell32.LogonUser(this.m_Username, this.m_Domain, this.m_Password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, ref tokenHandle); if (!logonSucceeded) { this.WriteToEventLog(string.Format("Impersonation failed for user: {0}", this.m_Username), EventLogEntryType.Error); // if the logon failed, get the error code and throw an exception throw new LogonException(string.Format(string.Format("User logon failed. Error Number: {0}", Marshal.GetLastWin32Error()))); } // if logon succeeds, create a WindowsIdentity instance m_impersonationIdentity = new WindowsIdentity(tokenHandle); this.WriteToEventLog(string.Format("Success! Impersonate user: {0}", this.m_Username), EventLogEntryType.Information); return(m_impersonationIdentity); } finally { } }