C# – Difference in ASP.Net impersonation methods

asp.netciisimpersonationnet

In this MSDN article on "How to implement impersonation in an ASP.NET application" they list 4 different ways to change the account that's used to execute the web request. Unfortunately, it doesn't describe the differences between these alternatives.

I'm trying to impersonate the IIS authenticated user to copy some files off their local machine. It works when I use the WIN32 api LogonUserA and impersonate a specific user. But I need the webapp to work with many users (I don't have an account that can access everyone's files).

I thought simply setting Impersonate = "true" and configuring IIS should work but something is different. When I check Environment.UserName it appears to be impersonating the correct account but I am getting "Access is denied" errors.

Anyone know the difference between these impersonation methods? Is it possible to impersonate the IIS authenticated user and then do some file operations with it?

Update: From the feedback I've been getting I need to be more clear about what I'm facing.

Environment setup:
IIS: disable anonymous authentication, enable integrated windows authentication
ASP.Net's web.config: authentication mode = "windows", impersonate = true, deny anonymous users

Suppose I'm accessing the page as "userA":

Scenario 1: impersonate the IIS Authenticated user

try{
  File.Copy(srcFile, destFile);   // Access Denied even though userA has access to srcFile.
} catch(Exception ex) {
...
}

Scenario 2: impersonate userA with LogonUser

try{
  // Impersonater is a wrapper around the WIN32 LogonUser API
  using(Impersonater imp = new Impersonator("domain", "userA", "pwd")) 
  {
    File.Copy(srcFile, destFile); // Works
  }
} catch(Exception ex) {
...
}

In both cases, I'm impersonating "userA".

Best Answer

Q: Anyone know the difference between these impersonation methods?

A: First some background on how IIS handles request.

There is a specific system user called IUSR_computername (default in IIS6) which the IIS-server uses to handle file access. And there is a process running on the IIS server called Aspnet_wp.exe which runs under an account called ASPNET or NetworkService.

So when a request is made to the server, the IIS reacts and if the request is to a ASP.NET application it passes the request to that process.

This means that if the IIS-server is setup to use the IUSR_computername (anonymous) access method. The server will use that account to process the request, and if it sees that it is an ASP.NET application it will transfer the request to the ASP.NET process.

By default impersonation is disabled, this means that the request will run under the ASPNET or NetworkService account when the ASP.NET process handles the request.

Now to the difference between the impersonation methods:

  • Impersonate the IIS authenticated account or user
    Uses an account that the IIS is setup to use. Usually IUSR_computername.
    Usage: <identity impersonate="true" />

  • Impersonation enabled for a specific identity
    Uses a specific account that is specified.
    Usage: <identity impersonate="true" userName="accountname" password="password" />

The third option is the default state, which is to disable impersonation.

Q: Is it possible to impersonate the IIS authenticated user and then do some file operations with it?

A: Depends on the priviliges of the IIS authenticated user. If the account has permission to manipulate files (NTFS permission in Windows), the answer would be yes.

Read more here:

  1. IIS Authentication
  2. ASP.NET Authentication
Related Topic