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

标签 c# .net iis iis-8 application-pool

我正在运行带有 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 然后选择你的主机名,然后打开配置。 enter image description here

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

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

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

关于c# - 如何以编程方式在 IIS 8 上设置 App Pool Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116530/

相关文章:

c++ - 我如何以编程方式找出 IIS 服务器有多少资源以及每个资源的流量..等等

database - RavenDB build #2127 客户端异常 : [Argument_StreamNotReadable] when trying to create a new Database

c# - 如何动态更改 wpf MVVM light 中用户控件中存在的按钮(单击)上的用户控件

c# - 我必须在简短的方法中使用 “using” 语句吗?

java - MVC.NET 的 @Html.Action 方法的 Spring Web 等效项是什么?

.net - 如何在 winforms 应用程序中测试本地化?

.net - Azure WebApp .NET SDK 区分小型、中型和大型实例

c# - MAC 导致 AESGCM 解密失败

c# - 将 C# double 与指定值进行比较是否安全?

asp.net - 在 ASP.NET 中自动生成机器 key