2014-11-25 125 views
0

我正在IIS 8上运行Windows Server 2012。我安装了iis 6元数据库兼容性。我一直在想如何改变应用程序池标识IIS 8.所有的我发现的例子与.NET 4.5找出针对IIS 6和7如何以编程方式设置应用程序池标识

以下是我有:

public class InternetInformationServices 
{ 
    public void SetApplicationPoolIdentity(string appPoolName, string domain, string username, string password) 
    { 
     try 
     { 
      string metabasePath = "IIS://Localhost/W3SVC/AppPools"; 

      DirectoryEntry myAppPool; 
      DirectoryEntry apppools = new DirectoryEntry(metabasePath); 
      myAppPool = apppools.Children.Find(appPoolName, "IIsApplicationPool"); 
      myAppPool.Invoke("AppPoolIdentityType", new Object[] { 3 }); 
      myAppPool.Invoke("WAMUserName", new Object[] { domain + @"\" + username }); 
      myAppPool.Invoke("WAMUserPass", new Object[] { password }); 
      myAppPool.Invoke("SetInfo", null); 
      myAppPool.CommitChanges(); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 
} 

的代码能够找到应用程序池,但只要我去调用设置任何操作:

myAppPool.Invoke("AppPoolIdentityType", new Object[] { 3 }); 
myAppPool.Invoke("WAMUserName", new Object[] { domain + @"\" + username }); 
myAppPool.Invoke("WAMUserPass", new Object[] { password }); 

我得到的内部异常的以下错误:

Value does not fall within the expected range. 

所以我不知道我缺少或什么是与IIS不同8.

+1

你应该摆脱的try/catch块。它没有任何好处 – 2014-11-25 16:07:17

回答

2

这里有一种方式来获得这种类型的代码段为iis元数据库中的大部分内容。我们使用这个过程来在我的办公室自动化脚本。

我会用你的有关指定凭据作为一个例子问题:

开放INETMGR,然后再选择你的主机名,然后打开配置。 enter image description here

然后选择system.applicationHost/applicationPools配置部分,并展开应用程序池收集,完成出关时窗: enter image description here 选择相应的应用程序池,改变identitytype到特定的用户,并设置用户和通。 enter image description here

现在点击生成脚本,不要应用更改。 enter image description here

FINALLY:所有你想要的东西,包括你的示例代码API善良:enter image description here

+0

哦,我可以不知道我可以做到这一点。感谢这个方便的技巧。 – HotRod 2016-03-09 21:07:02

相关问题