我使用下面的代码在我的应用程序的 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);
}
}
如何更改运行此应用程序池的用户凭据?
最佳答案
将以下内容添加到您的代码中创建 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;
您显然还需要将字符串变量username
和password
添加到您的CreateAppPool()
方法参数中。
如果您还不知道,您需要做的另一件事是确保您的应用程序池用户获得足够的权限来访问 IIS 元数据库、ASP.NET 临时文件夹等。您可以通过运行以下命令来完成此操作:
aspnet_regiis.exe -ga <username>
您可以在文件夹 %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727
中找到此工具。我通常只是使用 System.Diagnostics.Process 进行处理。
最后,应用程序池用户(至少)需要对应用程序的 Web 文件夹具有读取权限。
关于c# - 如何从 C# 更改 IIS 中应用程序池的用户名/密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/662634/