WindowsIdentity.Impersonate Method ()
.NET Framework 3.0
Impersonates the user represented by the WindowsIdentity object.
Namespace: System.Security.Principal
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
public WindowsImpersonationContext Impersonate ()
public function Impersonate () : WindowsImpersonationContext
Not applicable.
Return Value
A WindowsImpersonationContext object that represents the Windows user prior to impersonation; this 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.
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: After using Impersonate, it is important to call the Undo method to end the impersonation.The following example demonstrates how to impersonate a user using a provided Windows account token. For a complete example that shows how to obtain a Windows account token by calling the Win32 API LogonUser function and how to use that token to impersonate another user, see the WindowsImpersonationContext class.
void ImpersonateIdentity( IntPtr logonToken ) { // Retrieve the Windows identity using the specified token. WindowsIdentity^ windowsIdentity = gcnew WindowsIdentity( logonToken ); // Create a WindowsImpersonationContext object by impersonating the // Windows identity. WindowsImpersonationContext^ impersonationContext = windowsIdentity->Impersonate(); Console::WriteLine( "Name of the identity after impersonation: {0}.", WindowsIdentity::GetCurrent()->Name ); // Stop impersonating the user. impersonationContext->Undo(); // Check the identity name. Console::Write( "Name of the identity after performing an Undo on the" ); Console::WriteLine( " impersonation: {0}", WindowsIdentity::GetCurrent()->Name ); }
private static void ImpersonateIdentity(IntPtr logonToken)
{
// Retrieve the Windows identity using the specified token.
WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken);
// Create a WindowsImpersonationContext object by impersonating the
// Windows identity.
WindowsImpersonationContext impersonationContext
= windowsIdentity.Impersonate();
Console.WriteLine("Name of the identity after impersonation: "
+ WindowsIdentity.GetCurrent().get_Name() + ".");
// Stop impersonating the user.
impersonationContext.Undo();
// Check the identity name.
Console.Write("Name of the identity after performing an Undo on the");
Console.WriteLine(" impersonation: "
+ WindowsIdentity.GetCurrent().get_Name());
} //ImpersonateIdentity
Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: