我使用下面的代码在我的应用程序的Installer类中创建一个新的应用程序池:
private static void CreateAppPool(string serverName, string appPoolName) { // metabasePath is of the form "IIS:///W3SVC/AppPools" // for example "IIS://localhost/W3SVC/AppPools" // appPoolName is of the form " ", 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
您可以在文件夹中找到此工具%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727
.我通常只是在使用System.Diagnostics.Process
.
最后,应用程序池用户将需要(至少)应用程序的Web文件夹的读取权限.