This documentation is archived and is not being maintained.

WindowsIdentity::Impersonate Method (IntPtr)

Updated: December 2010

Impersonates the user represented by the specified user token.

Namespace:  System.Security.Principal
Assembly:  mscorlib (in mscorlib.dll)

[SecurityPermissionAttribute(SecurityAction::Demand, Flags = SecurityPermissionFlag::ControlPrincipal)]
public:
static WindowsImpersonationContext^ Impersonate(
	IntPtr userToken
)

Parameters

userToken
Type: System::IntPtr

The handle of a Windows account token. This token is usually retrieved through a call to unmanaged code, such as a call to the Win32 API LogonUser function.

Return Value

Type: System.Security.Principal::WindowsImpersonationContext
A WindowsImpersonationContext object that represents the Windows user prior to impersonation; this object can be used to revert to the original user's context.

ExceptionCondition
UnauthorizedAccessException

Windows returned the Windows NT status code STATUS_ACCESS_DENIED.

OutOfMemoryException

There is insufficient memory available.

SecurityException

The caller does not have the correct permissions.

On Windows NT platforms, the current user must have sufficient rights to allow impersonation.

NoteNote:

Calling the Impersonate(IntPtr) method with a userToken value of Zero is equivalent to calling the Win32 RevertToSelf function. If another user is currently being impersonated, control reverts to the original user.

For more information about calls to unmanaged code, see Consuming Unmanaged DLL Functions.

Notes to Implementers:

Because Windows 98 and Windows Millennium Edition (Windows Me) platforms do not have user tokens, impersonation cannot take place on those platforms.

Notes to Callers:

After using Impersonate, it is important to call the Undo method to end the impersonation.

The following example demonstrates how to obtain a Windows account token by calling the unmanaged Win32 LogonUser function, and how to use that token to impersonate another user and then revert to the original identity.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

December 2010

Updated the example.

Customer feedback.

May 2009

Added a permission.

Information enhancement.

Show: