Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 Impersonate Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
WindowsIdentity..::.Impersonate Method

Impersonates the user represented by the WindowsIdentity object.

Namespace:  System.Security.Principal
Assembly:  mscorlib (in mscorlib.dll)
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.
ExceptionCondition
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 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 );
}


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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker