C# Class Tools.Impersonator

Impersonation of a user. Allows to execute code under another user context. Please note that the account that instantiates the Impersonator class needs to have the 'Act as part of operating system' privilege set.
This class is based on the information in the Microsoft knowledge base article http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158 Encapsulate an instance into a using-directive like e.g.: ... using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) ) { ... [code that executes under the new context] ... } ... Please contact the author Uwe Keim (mailto:[email protected]) for questions regarding this class.
Inheritance: IDisposable
Datei anzeigen Open project: davidadsit/TeamFoundationServerDesktopBuildMonitor Class Usage Examples

Public Methods

Method Description
Dispose ( ) : void
Impersonator ( string userName, string domainName, string password ) : System

Constructor. Starts the impersonation with the given credentials. Please note that the account that instantiates the Impersonator class needs to have the 'Act as part of operating system' privilege set.

Private Methods

Method Description
CloseHandle ( IntPtr handle ) : bool
DuplicateToken ( IntPtr hToken, int impersonationLevel, IntPtr &hNewToken ) : int
ImpersonateValidUser ( string userName, string domain, string password ) : void

Does the actual impersonation.

LogonUser ( string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, IntPtr &phToken ) : int
RevertToSelf ( ) : bool
UndoImpersonation ( ) : void

Reverts the impersonation.

Method Details

Dispose() public method

public Dispose ( ) : void
return void

Impersonator() public method

Constructor. Starts the impersonation with the given credentials. Please note that the account that instantiates the Impersonator class needs to have the 'Act as part of operating system' privilege set.
public Impersonator ( string userName, string domainName, string password ) : System
userName string The name of the user to act as.
domainName string The domain name of the user to act as.
password string The password of the user to act as.
return System