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 )
- 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 ValueType: System.Security.Principal::WindowsImpersonationContext
An object that represents the Windows user prior to impersonation; this object can be used to revert to the original user's context.
On Windows NT platforms, the current user must have sufficient rights to allow impersonation.
Calling the 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 Microsoft Windows 98 and Windows Millennium Edition (Windows Me) platforms do not have user tokens, impersonation cannot take place on those platforms.Notes to Callers
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.
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.