我怎样才能改变ApplicationPool的IIS从C#的用户名/密码?用户名、密码、ApplicationPool、IIS

2023-09-03 01:16:08 作者:稳中求胜,@

我用下面的code创建的安装程序类我的应用程序的新应用程序池:

I am using the code below to create a new application pool in the Installer class of my application:

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

我怎样才能更改用户凭据在此应用程序池运行?

How can I change the user credentials under which this application pool is running?

推荐答案

以下内容添加到您的code只是在其中创建 newpool 行之后:

Add the following to your code just after the line where you create 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;

您会明显地需要添加字符串变量用户名密码 CreateAppPool()方法参数为好。

You'll obviously need to add the string variables username and password to your CreateAppPool() method parameters as well.

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

Another thing you need to do, if you weren't already aware, is make sure your application pool user gets sufficient rights to access the IIS metabase, ASP.NET temp folders etc. You can do this by running the following command:

aspnet_regiis.exe -ga <username>

您可以找到该文件夹​​中的这个工具%SYSTEMROOT%\ Microsoft.NET \框架\ V2.0.50727 。我通常只是掏出使用的System.Diagnostics.Process

You can find this tool in the folder %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727. I usually just shell out using System.Diagnostics.Process.

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

And finally, the application pool user will need (at least) read rights on the web folder for the app.

千电子伏