5

I'm trying to create application pool using the ServerManager class. This is my code:

using (ServerManager serverManager = new ServerManager())  {
   if (!serverManager.ApplicationPools.Any(p => p.Name == poolName))  {
     ApplicationPool newPool = serverManager.ApplicationPools.Add(poolName);
     newPool.ManagedRuntimeVersion = "v4.0";
     newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
     newPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
     newPool.ProcessModel.UserName = user;
     newPool.ProcessModel.Password = pass;
     serverManager.CommitChanges();
   }
}

The application pool gets created, but no identity is set for it - the identity column in the application pools table in IIS Manager is blank. What am i doing wrong?

User is in the form of domain\username, and the credentials passed are correct (another part of code checks for those).

StefanS
  • 1,089
  • 1
  • 11
  • 38
user2073333
  • 152
  • 1
  • 9

1 Answers1

2

Take a look to this code. It is working for me:

public static ApplicationPool GetOrCreateApplicationPool(ServerManager serverManager, string applicationPoolName, string userName, SecureString password)
{
    if (!serverManager.ApplicationPools.Any(p => p.Name.Equals(applicationPoolName)))
    {
        ApplicationPool appPool = serverManager.ApplicationPools.CreateElement();
        appPool.Name = applicationPoolName;
        appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
        appPool.ManagedRuntimeVersion = "v4.0";

        if(password != null)
        {
            if (!(string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password.ToPlainTextString())))
            {
                appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
                appPool.ProcessModel.UserName = userName;
                appPool.ProcessModel.Password = password.ToPlainTextString();
            }
        }                

        serverManager.ApplicationPools.Add(appPool);
        serverManager.CommitChanges();
    }

    return serverManager.ApplicationPools.First(p => p.Name.Equals(applicationPoolName));
}

Of course there are some additional methods such extensions that might cause this piece of code is not able to run but the idea is there and this is currently running on my application.

Manuel
  • 21
  • 4