.NET Framework Class Library
WindowsIdentity..::.Impersonate Method

Impersonates the user represented by the WindowsIdentity object.

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

Visual Basic (Declaration)
Public Overridable Function Impersonate As WindowsImpersonationContext
Visual Basic (Usage)
Dim instance As WindowsIdentity
Dim returnValue As WindowsImpersonationContext

returnValue = instance.Impersonate()
C#
public virtual WindowsImpersonationContext Impersonate()
Visual C++
public:
virtual WindowsImpersonationContext^ Impersonate()
JScript
public function Impersonate() : WindowsImpersonationContext

Return Value

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

ExceptionCondition
InvalidOperationException

An anonymous identity attempted to perform an impersonation.

SecurityException

A Win32 error occurred.

Remarks

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.

Examples

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.

Visual Basic
Private Sub ImpersonateIdentity(ByVal logonToken As IntPtr)
    ' Retrieve the Windows identity using the specified token.
    Dim windowsIdentity As New WindowsIdentity(logonToken)

    ' Create a WindowsImpersonationContext object by impersonating the
    ' Windows identity.
    Dim impersonationContext As WindowsImpersonationContext
    impersonationContext = windowsIdentity.Impersonate()

    WriteLine("Name of the identity after impersonation: " + _
        windowsIdentity.GetCurrent().Name + ".")

    ' Stop impersonating the user.
    impersonationContext.Undo()

    ' Check the identity.
    WriteLine("Name of the identity after performing an Undo on the " + _
        "impersonation: " + windowsIdentity.GetCurrent().Name + ".")
End Sub
C#
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().Name + ".");
    Console.WriteLine(windowsIdentity.ImpersonationLevel);
    // 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().Name);
}
Visual C++
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 );
}

Platforms

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.
Version Information

.NET Framework

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

Reference

Tags :


Page view tracker