20 December 2014

Fetch Application Pool Identity

Web Application requires the database operation to be performed by service account which is configured in AppPool.
When ever the user hits the web application the request is received at IIS and CRUD operation is performed against database.

Here's my code snippet to have transaction performed by APPPool Identity

Add this code

using System.Runtime.InteropServices;
using System.Security.Principal;

private static WindowsIdentity _appPoolIdentity;
public static WindowsIdentity AppPoolIdentity
   get { WindowsIdentity result;
         if (_appPoolIdentity != null){result = _appPoolIdentity;}
         else {result = (_appPoolIdentity = GetAppPoolIdentity());}
         return result;

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool RevertToSelf();
private static WindowsIdentity GetAppPoolIdentity()
       WindowsIdentity identity = null;
       Win32Exception win32Exception = null;
       Thread thread = new Thread(delegate(object o)
if (!Config.RevertToSelf())
       int lastWin32Error = Marshal.GetLastWin32Error();
       win32Exception = new Win32Exception(lastWin32Error);
  identity = WindowsIdentity.GetCurrent();
if (win32Exception != null)
       throw win32Exception;
return identity;

From your code just call the

       //your Execution Block under AppPool Id

Hope this Helps you

Rate Now: