Microsoft KB Archive/306158

= How to implement impersonation in an ASP.NET application =

Article ID: 306158

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q306158



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Web.Security
 * System.Security.Principal
 * System.Runtime.InteropServices



SUMMARY
This article describes different ways to implement impersonation in an ASP.NET application.



MORE INFORMATION
If you want to impersonate a user on a thread in ASP.NET, you can use one of the following methods, based on your requirments:
 * Impersonate the IIS authenticated account or user
 * Impersonate a specific user for all the requests of an ASP.NET application
 * Impersonate the authenticating user in code
 * Impersonate a specific user in code

Note You can use the following code to determine what user the thread is executing as: System.Security.Principal.WindowsIdentity.GetCurrent.Name

Impersonate the IIS Authenticated Account or User
To impersonate the Microsoft Internet Information Services (IIS) authenticating user on every request for every page in an ASP.NET application, you must include an   tag in the Web.config file of this application and set the impersonate attribute to true. For example:  back to the top

Impersonate a Specific User for All the Requests of an ASP.NET Application
To impersonate a specific user for all the requests on all pages of an ASP.NET application, you can specify the userName and password attributes in the   tag of the Web.config file for that application. For example:  Note The identity of the process that impersonates a specific user on a thread must have the &quot;Act as part of the operating system&quot; privilege. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account does not have the required privileges to impersonate a specific user. You receive an error message if you try to impersonate a specific user. This information applies only to the .NET Framework 1.0. This privilege is not required for the .NET Framework 1.1.

To work around this problem, use one of the following methods:
 * Grant the &quot;Act as part of the operating system&quot; privilege to the ASPNET account (the least privileged account).

Note Although you can use this method to work around the problem, Microsoft does not recommend this method.
 * Change the account that the Aspnet_wp.exe process runs under to the System account in the  configuration section of the Machine.config file.

back to the top

Impersonate the Authenticating User in Code
To impersonate the authenticating user (User.Identity) only when you run a particular section of code, you can use the code to follow. This method requires that the authenticating user identity is of type WindowsIdentity.

Visual Basic .NET Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity

currentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity) impersonationContext = currentWindowsIdentity.Impersonate

'Insert your code that runs under the security context of the authenticating user here.

impersonationContext.Undo Visual C# .NET System.Security.Principal.WindowsImpersonationContext impersonationContext; impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate;

//Insert your code that runs under the security context of the authenticating user here.

impersonationContext.Undo; Visual J# .NET System.Security.Principal.WindowsImpersonationContext impersonationContext; impersonationContext = ((System.Security.Principal.WindowsIdentity)get_User.get_Identity).Impersonate;

//Insert your code that runs under the security context of the authenticating user here.

impersonationContext.Undo; back to the top

Impersonate a Specific User in Code
To impersonate a specific user only when you run a particular section of code, use the following code:

Visual Basic .NET <%@ Page Language=&quot;VB&quot; %> <%@ Import Namespace = &quot;System.Web&quot; %> <%@ Import Namespace = &quot;System.Web.Security&quot; %> <%@ Import Namespace = &quot;System.Security.Principal&quot; %> <%@ Import Namespace = &quot;System.Runtime.InteropServices&quot; %>

Dim LOGON32_LOGON_INTERACTIVE As Integer = 2 Dim LOGON32_PROVIDER_DEFAULT As Integer = 0

Dim impersonationContext As WindowsImpersonationContext

Declare Function LogonUserA Lib &quot;advapi32.dll&quot; (ByVal lpszUsername As String, _                       ByVal lpszDomain As String, _                        ByVal lpszPassword As String, _                        ByVal dwLogonType As Integer, _                        ByVal dwLogonProvider As Integer, _                        ByRef phToken As IntPtr) As Integer

Declare Auto Function DuplicateToken Lib &quot;advapi32.dll&quot; ( _                       ByVal ExistingTokenHandle As IntPtr, _                        ByVal ImpersonationLevel As Integer, _                        ByRef DuplicateTokenHandle As IntPtr) As Integer

Declare Auto Function RevertToSelf Lib &quot;advapi32.dll&quot; As Long Declare Auto Function CloseHandle Lib &quot;kernel32.dll&quot; (ByVal handle As IntPtr) As Long

Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) If impersonateValidUser(&quot;username&quot;, &quot;domain&quot;, &quot;password&quot;) Then 'Insert your code that runs under the security context of a specific user here. undoImpersonation Else 'Your impersonation failed. Therefore, include a fail-safe mechanism here. End If End Sub

Private Function impersonateValidUser(ByVal userName As String, _ ByVal domain As String, ByVal password As String) As Boolean

Dim tempWindowsIdentity As WindowsIdentity Dim token As IntPtr = IntPtr.Zero Dim tokenDuplicate As IntPtr = IntPtr.Zero impersonateValidUser = False

If RevertToSelf Then If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,                     LOGON32_PROVIDER_DEFAULT, token) <> 0 Then If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then tempWindowsIdentity = New WindowsIdentity(tokenDuplicate) impersonationContext = tempWindowsIdentity.Impersonate If Not impersonationContext Is Nothing Then impersonateValidUser = True End If           End If        End If    End If    If Not tokenDuplicate.Equals(IntPtr.Zero) Then CloseHandle(tokenDuplicate) End If   If Not token.Equals(IntPtr.Zero) Then CloseHandle(token) End If End Function

Private Sub undoImpersonation impersonationContext.Undo End Sub Visual C# .NET <%@ Page Language=&quot;C#&quot;%> <%@ Import Namespace = &quot;System.Web&quot; %> <%@ Import Namespace = &quot;System.Web.Security&quot; %> <%@ Import Namespace = &quot;System.Security.Principal&quot; %> <%@ Import Namespace = &quot;System.Runtime.InteropServices&quot; %>

public const int LOGON32_LOGON_INTERACTIVE = 2; public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport(&quot;advapi32.dll&quot;)] public static extern int LogonUserA(String lpszUserName,    String lpszDomain,    String lpszPassword,    int dwLogonType,     int dwLogonProvider,    ref IntPtr phToken); [DllImport(&quot;advapi32.dll&quot;, CharSet=CharSet.Auto, SetLastError=true)] public static extern int DuplicateToken(IntPtr hToken,    int impersonationLevel,      ref IntPtr hNewToken); [DllImport(&quot;advapi32.dll&quot;, CharSet=CharSet.Auto, SetLastError=true)] public static extern bool RevertToSelf;

[DllImport(&quot;kernel32.dll&quot;, CharSet=CharSet.Auto)] public static extern bool CloseHandle(IntPtr handle);

public void Page_Load(Object s, EventArgs e) { if(impersonateValidUser(&quot;username&quot;, &quot;domain&quot;, &quot;password&quot;)) {       //Insert your code that runs under the security context of a specific user here. undoImpersonation; }   else {       //Your impersonation failed. Therefore, include a fail-safe mechanism here. } }

private bool impersonateValidUser(String userName, String domain, String password) {   WindowsIdentity tempWindowsIdentity; IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero;

if(RevertToSelf) {       if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) {           if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) {               tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); impersonationContext = tempWindowsIdentity.Impersonate; if (impersonationContext != null) {                   CloseHandle(token); CloseHandle(tokenDuplicate); return true; }           }        }     }    if(token!= IntPtr.Zero) CloseHandle(token); if(tokenDuplicate!=IntPtr.Zero) CloseHandle(tokenDuplicate); return false; }

private void undoImpersonation {   impersonationContext.Undo; } Visual J# .NET <%@ Page language=&quot;VJ#&quot; %> <%@ Import Namespace=&quot;System.Web&quot; %> <%@ Import Namespace=&quot;System.Web.Security&quot; %> <%@ Import Namespace=&quot;System.Security.Principal&quot; %> <%@ Import Namespace=&quot;System.Runtime.InteropServices&quot; %>

public static int LOGON32_LOGON_INTERACTIVE = 2; public static int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

/** @attribute DllImport(&quot;advapi32.dll&quot;) */ public static native int LogonUserA(String lpszUserName,    String lpszDomain,     String lpszPassword,    int dwLogonType,     int dwLogonProvider,     System.IntPtr[] phToken);

/** @attribute DllImport(&quot;advapi32.dll&quot;, CharSet=CharSet.Auto, SetLastError=true) */ public static native int DuplicateToken(System.IntPtr hToken,   int impersonationLevel,    System.IntPtr[] hNewToken);

/** @attribute DllImport(&quot;kernel32.dll&quot;,CharSet=CharSet.Auto) */ public static native boolean CloseHandle(System.IntPtr[] handle);

/** @attribute DllImport(&quot;advapi32.dll&quot;,    CharSet=CharSet.Auto,SetLastError=true) */ public static native boolean RevertToSelf;

public void Page_Load(Object s, System.EventArgs e) { if(impersonateValidUser(&quot;username&quot;, &quot;domain&quot;, &quot; password&quot;)) {       //Insert your code that runs under the security context of a specific user here. undoImpersonation; }   else {       //Your impersonation failed. Therefore, include a fail-safe mechanism here. } }

private boolean impersonateValidUser(String userName, String domain, String password) {   WindowsIdentity tempWindowsIdentity; System.IntPtr[] token = new System.IntPtr[1]; System.IntPtr[] tokenDuplicate = new System.IntPtr[1];

if(RevertToSelf) {       if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) != 0) {           if(DuplicateToken(token[0], 2, tokenDuplicate) != 0) {               tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[0]); impersonationContext = tempWindowsIdentity.Impersonate; if (impersonationContext != null) {                   CloseHandle(tokenDuplicate); CloseHandle(token); return true; }                          }                   }     }    if(!token[0].Equals(System.IntPtr.Zero)) CloseHandle(token); if(!tokenDuplicate[0].Equals(System.IntPtr.Zero)) CloseHandle(tokenDuplicate); return false;

}

private void undoImpersonation {   impersonationContext.Undo; } Note The identity of the process that impersonates a specific user on a thread must have the &quot;Act as part of the operating system&quot; privilege if the Aspnet_wp.exe process is running on a Microsoft Windows 2000-based computer. The &quot;Act as part of the operating system&quot; privilege is not required if the Aspnet_wp.exe process is running on a Windows XP-based computer or on a Windows Server 2003-based computer. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account does not have the required privileges to impersonate a specific user. You receive an error message if you try to impersonate a specific user. .

To work around this problem, use one of the following methods:
 * Grant the &quot;Act as part of the operating system&quot; privilege to the ASPNET account.

Note We do not recommend this method to work around the problem.
 * Change the account that the Aspnet_wp.exe process runs under to the System account in the  configuration section of the Machine.config file.

back to the top

