如何以编程方式设置应用程序池标识
我正在IIS 8上运行Windows Server 2012。我安装了iis 6元数据库兼容性。我一直在想如何改变应用程序池标识IIS 8.所有的我发现的例子与.NET 4.5找出针对IIS 6和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;
}
}
}
的代码能够找到应用程序池,但只要我去调用设置任何操作:
myAppPool.Invoke("AppPoolIdentityType", new Object[] { 3 });
myAppPool.Invoke("WAMUserName", new Object[] { domain + @"\" + username });
myAppPool.Invoke("WAMUserPass", new Object[] { password });
我得到的内部异常的以下错误:
Value does not fall within the expected range.
所以我不知道我缺少或什么是与IIS不同8.
试试这个(从How can I change the username/password of an ApplicationPool in IIS from C#?)
myAppPool .Properties["AppPoolIdentityType"].Value = 3;
myAppPool .Properties["WAMUserName"].Value = Environment.MachineName + @"\" + username;
myAppPool .Properties["WAMUserPass"].Value = password;
这对我工作谢谢。 – HotRod 2014-11-25 16:08:42
这里有一种方式来获得这种类型的代码段为iis元数据库中的大部分内容。我们使用这个过程来在我的办公室自动化脚本。
我会用你的有关指定凭据作为一个例子问题:
然后选择system.applicationHost/applicationPools配置部分,并展开应用程序池收集,完成出关时窗: 选择相应的应用程序池,改变identitytype到特定的用户,并设置用户和通。
哦,我可以不知道我可以做到这一点。感谢这个方便的技巧。 – HotRod 2016-03-09 21:07:02
你应该摆脱的try/catch块。它没有任何好处 – 2014-11-25 16:07:17