WindowsIdentity::Impersonate Method (IntPtr)
Impersonates the user represented by the specified user token.
Assembly: mscorlib (in mscorlib.dll)
[SecurityPermissionAttribute(SecurityAction::Demand, Flags = SecurityPermissionFlag::NoFlags|SecurityPermissionFlag::UnmanagedCode|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::WindowsImpersonationContextAn object that represents the Windows user prior to impersonation; this object can be used to revert to the original user's context.
| Exception | Condition |
|---|---|
| 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.
Note |
|---|
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 ImplementersBecause Microsoft Windows 98 and Windows Millennium Edition (Windows Me) platforms do not have user tokens, impersonation cannot take place on those platforms.
Notes to CallersAfter using Impersonate, it is important to call the Undo method to end the impersonation.
- SecurityPermission
for the ability to manipulate the principal object and access unmanaged code. Associated enumerations: SecurityPermissionFlag::ControlPrincipal and SecurityPermissionFlag::UnmanagedCode
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note