如何以编程方式设置应用程序池标识 [英] How to programmatically set App Pool Identity
本文介绍了如何以编程方式设置应用程序池标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行带有IIS 8的Windows Server 2012.我安装了iis 6配置数据库兼容性。我一直试图找出.Net 4.5如何更改IIS 8的应用程序池标识。我找到的所有示例都是针对IIS 6和7。
I'm running Windows Server 2012 with IIS 8. I have iis 6 metabase compatibility installed. I have been trying to figure out with .Net 4.5 on how to change the App Pool Identity for IIS 8. All of the examples I found are for IIS 6 and 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;
}
}
}
代码能找到应用程序池,但是一旦我进入Invoke设置以下任何一项:
The code is able to find the App Pool but as soon as I go to Invoke to set any of the following:
myAppPool.Invoke("AppPoolIdentityType", new Object[] { 3 });
myAppPool.Invoke("WAMUserName", new Object[] { domain + @"\" + username });
myAppPool.Invoke("WAMUserPass", new Object[] { password });
我在内部异常上收到以下错误:
I get the following error on the inner exception:
Value does not fall within the expected range.
所以我不确定我缺少什么或与IIS 8有什么不同。
So I'm not sure what I'm missing or what is different with IIS 8.
推荐答案
试试这个(来自如何在C#中更改IIS中ApplicationPool的用户名/密码?)
myAppPool .Properties["AppPoolIdentityType"].Value = 3;
myAppPool .Properties["WAMUserName"].Value = Environment.MachineName + @"\" + username;
myAppPool .Properties["WAMUserPass"].Value = password;
这篇关于如何以编程方式设置应用程序池标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文