Share via


Changing the Windows Azure Web Host application pool identity

Sometimes, need may arise to change the app pool identity of the azure web host you are working with locally on Development Fabric/Compute Emulator

By default, the web host runs under a temporary application pool with NTAUTHORITY\NETWORK SERVICE account. This account may not have access to some key local/remote resources.(e.g. using the proxy server to resolve addresses or send messages). In that case, you can switch the current application pool identity using the snippet below.

You'll need to add a reference to Microsoft.Web.Administration.dll which is present in %SystemRoot%\system32\inetsrv for following to work. Note that this code should be called at WebRole.OnStart() before base.OnStart() is called. 

            using (ServerManager serverManager = new ServerManager())

            {

                string appPoolName = serverManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_" + webApplicationProjectName].Applications.First().ApplicationPoolName;

               

                var appPool = serverManager.ApplicationPools[appPoolName];               

                appPool.ProcessModel.UserName = appPoolUserName;

                appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;

                appPool.ProcessModel.Password = appPoolPassword;

               

                serverManager.CommitChanges();

            }