Click or drag to resize

ExtensionsDirectoryServicesImpersonate Method

Impersonates the given user using Win32 API.

Namespace:  Cauldron
Assembly:  Cauldron.Win32.Impersonation (in Cauldron.Win32.Impersonation.dll) Version: 3.2.0.1 (3.2.0.1)
Syntax
C#
public static WindowsImpersonationContext Impersonate(
	this PrincipalContext principalContext,
	string username,
	string password,
	LogonType logonType
)

Parameters

principalContext
Type: System.DirectoryServices.AccountManagementPrincipalContext
The principal context of the user
username
Type: SystemString
The user name of the user to impersonate
password
Type: SystemString
The password of the user to impersonate
logonType
Type: CauldronLogonType
The type of logon operation to perform.

Return Value

Type: WindowsImpersonationContext
A WindowsImpersonationContext of the impersonation

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type PrincipalContext. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullExceptionusername is null
ArgumentNullExceptionpassword is null
ArgumentExceptionusername is empty
ArgumentExceptionpassword is empty
Examples
using (var context = new PrincipalContext(ContextType.Domain))
{
    context.Impersonate("billgates", "superSafePassword!!!!1111", LogonType.Network);

    // Do anything in the context of the user 'billgates'
    this.database.DeleteAll();
}
See Also