Getting the IIS web server application pool identity in C# code inside an ASP.NET MVC application

Posted: (EET/GMT+2)

 

In the Microsoft IIS web server, web applications run in the context of an application pool. When the application pool is defined, it is assigned an identity, which is a Windows user account, either local or a domain account.

The identity of the application pool defines the user account under which your web applications run. Sometimes, it is useful to be able to find out the current application pool user, for example for debugging purposes.

If you are running and ASP.NET MVC web application, here are two ways to get the application pool identity from your C# code. First, you can use the System.Environment class:

string appPoolIdentity = System.Environment.UserDomainName + "\\" +
  System.Environment.UserName;

Or alternatively, you can query the current thread's user context:

using System.Security.Principal;
...
string username = WindowsIdentity.GetCurrent().Name;

Hope this helps!