ServerManager,创建应用程序池,未设置标识

时间:2016-04-01 12:25:11

标签: c# application-pool servermanager

我正在尝试使用ServerManager类创建应用程序池。这是我的代码:

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();
   }
}

创建应用程序池,但没有为其设置标识 - IIS管理器中应用程序池表中的标识列为空。 我做错了什么?

用户采用域\用户名的形式,并且传递的凭据是正确的(代码的另一部分会检查这些凭据)。

1 个答案:

答案 0 :(得分:0)

看看这段代码。它对我有用:

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));
}

当然还有一些额外的方法,例如扩展,可能会导致这段代码无法运行,但想法是存在的,并且目前正在我的应用程序上运行。