【问题标题】:How can I change the username/password of an ApplicationPool in IIS from C#?如何从 C# 更改 IIS 中 ApplicationPool 的用户名/密码?
【发布时间】:2010-10-14 08:10:41
【问题描述】:

我正在使用下面的代码在我的应用程序的 Installer 类中创建一个新的应用程序池:

private static void CreateAppPool(string serverName, string appPoolName)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
    //    for example "IIS://localhost/W3SVC/AppPools" 
    //  appPoolName is of the form "<name>", for example, "MyAppPool"
    string metabasePath = string.Format("IIS://{0}/W3SVC/AppPools", serverName);
    Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);
    try
    {
        DirectoryEntry apppools = new DirectoryEntry(metabasePath);
        DirectoryEntry newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
        newpool.CommitChanges();
        Console.WriteLine("AppPool created.");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
    }
}

如何更改运行此应用程序池的用户凭据?

【问题讨论】:

    标签: c# .net iis wmi iis-6


    【解决方案1】:

    将以下内容添加到您创建 newpool 的行之后:

    DirectoryEntry newpool = 
                apppools.Children.Add(appPoolName, "IIsApplicationPool");
    // Add this:
    newpool.Properties["AppPoolIdentityType"].Value = 3;
    newpool.Properties["WAMUserName"].Value = 
                Environment.MachineName + @"\" + username;
    newpool.Properties["WAMUserPass"].Value = password;
    

    您显然还需要将字符串变量 usernamepassword 添加到您的 CreateAppPool() 方法参数中。

    如果您还不知道,您需要做的另一件事是确保您的应用程序池用户获得足够的权限来访问 IIS 元数据库、ASP.NET 临时文件夹等。您可以通过运行以下命令来执行此操作:

    aspnet_regiis.exe -ga <username>
    

    您可以在文件夹%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727 中找到此工具。我通常只是使用System.Diagnostics.Process 进行支付。

    最后,应用程序池用户将需要(至少)对应用程序的 web 文件夹具有读取权限。

    【讨论】:

      【解决方案2】:
      aspnet_regiis.exe -ga <username>
      

      可能不是最好的命令。

      您应该将 APPPool 用户添加到 IIS_WPG 组并使用该组授予权限。

      【讨论】:

      • 您确实需要使用 aspnet_regiis.exe -ga 来分配适当的工作进程身份权限/权限。这就是它的设计目的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      相关资源
      最近更新 更多