This documentation is archived and is not being maintained.

WindowsIdentity.Impersonate Method ()

Impersonates the user represented by the WindowsIdentity object.

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

public virtual WindowsImpersonationContext Impersonate ()
public WindowsImpersonationContext Impersonate ()
public function Impersonate () : WindowsImpersonationContext

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.

Exception typeCondition

InvalidOperationException

An anonymous identity attempted to perform an impersonation.

SecurityException

A Win32 error occurred.

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 showing how to obtain a Windows account token through a call to the Win32 API LogonUser function, and use that token to impersonate another user, see the WindowsImpersonationContext class.

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);
}

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: