Skip to main content
 首页 » 编程设计

c#之如何以编程方式在 IIS 8 上设置 App Pool Identity

2024年02月06日30kuangbin

我正在运行带有 IIS 8 的 Windows Server 2012。我安装了 IIS 6 元数据库兼容性。我一直在尝试弄清楚如何使用 .Net 4.5 更改 IIS 8 的 App Pool Identity - 我找到的所有示例都是针对 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 有什么不同。

请您参考如下方法:

这是为 iis 元数据库中的大多数内容获取此类代码片段的方法。我们使用这个过程在我的办公室编写自动化脚本。

我将以您关于指定凭据的问题为例:

首先打开 inetmgr 然后选择你的主机名,然后打开配置。

然后选择 system.applicationHost/applicationPools 配置部分并展开应用程序池集合,完成后退出窗口: 选择适当的应用程序池,将身份类型更改为特定用户并设置用户并通过。

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

最后:所有你想要的 api 优点,包括你的示例代码: