To impersonate the Internet Information Services (IIS) authenticating user on every request for every page in an ASP.NET application, you must include an <identity> tag in the Web.config file of this application and set the impersonate attribute to true. For example:
XML
<identityimpersonate="true" />
Impersonate a specific user for all 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 <identity> tag of the Web.config file for that application. For example:
The identity of the process that impersonates a specific user on a thread must have the Act as part of the operating system privilege. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account doesn't 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 Act as part of the operating system privilege to the ASPNET account (the least privileged account).
Бележка
Although you can use this method to work around the problem, Microsoft doesn't recommend this method.
Change the account that the Aspnet_wp.exe process runs under to the System account in the <processModel> configuration section of the Machine.config file.
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
VB
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
C#
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();
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
Visual Basic Script
<%@ Page Language="VB" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script runat=server>
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2Dim LOGON32_PROVIDER_DEFAULT As Integer = 0Dim impersonationContext As WindowsImpersonationContext
Declare Function LogonUserA Lib "advapi32.dll" (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 "advapi32.dll" ( _
ByVal ExistingTokenHandle As IntPtr, _
ByVal ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
PublicSub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If impersonateValidUser("username", "domain", "password") 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.EndIfEndSubPrivateFunction 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 = FalseIf RevertToSelf() ThenIf LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, token) <> 0ThenIf DuplicateToken(token, 2, tokenDuplicate) <> 0Then
tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
impersonationContext = tempWindowsIdentity.Impersonate()
IfNot impersonationContext IsNothingThen
impersonateValidUser = TrueEndIfEndIfEndIfEndIfIfNot tokenDuplicate.Equals(IntPtr.Zero) Then
CloseHandle(tokenDuplicate)
EndIfIfNot token.Equals(IntPtr.Zero) Then
CloseHandle(token)
EndIfEndFunctionPrivateSub undoImpersonation()
impersonationContext.Undo()
EndSub
</script>
The identity of the process that impersonates a specific user on a thread must have the Act as part of the operating system privilege if the Aspnet_wp.exe process is running on a Windows 2000-based computer. The Act as part of the operating system privilege isn't 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 doesn't 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 Act as part of the operating system privilege to the ASPNET account.
Бележка
We don't 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 <processModel> configuration section of the Machine.config file.
This article provides resolutions for access denied error that occurs when a web application writes data to the ASP.NET App_Data folder in IIS 7.5 and later versions.