[英]How to programmatically set App Pool Identity on IIS 8
I'm running Windows Server 2012 with IIS 8. I have IIS 6 Metabase Compatibility installed. 我正在使用IIS 8运行Windows Server2012。安装了IIS 6元数据库兼容性。 I have been trying to figure out how to change the App Pool Identity for IIS 8 with .Net 4.5 - all of the examples I found are for IIS 6 and 7.
我一直在尝试找出如何使用.Net 4.5更改IIS 8的应用程序池标识-我发现的所有示例都是针对IIS 6和7的。
Here is what I have: 这是我所拥有的:
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;
}
}
}
The code is able to find the App Pool, but as soon as I invoke it 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.
值不在预期范围内。
I'm not sure what I'm missing, or what is different with IIS 8. 我不确定我缺少什么,或者IIS 8有什么不同。
Here's a way to get this type of code snippet for most things in the iis metabase. 这是在iis配置数据库中获取大多数功能的这种代码段的一种方法。 we use this process to script automation at my office.
我们使用此过程在我的办公室编写自动化脚本。
I'll use your question about specifying credentials as an example: 我将以您有关指定凭据的问题为例:
open inetmgr first and then select your hostname and then open the config. 首先打开inetmgr,然后选择您的主机名,然后打开配置。
then select the system.applicationHost/applicationPools config section and expand the app pools collection, when done exit the window: 然后选择system.applicationHost / applicationPools配置部分并展开应用程序池集合,完成后退出窗口:
select the appropriate application pool, change the identitytype to specific user and set the user and pass.
选择适当的应用程序池,将标识类型更改为特定用户,然后设置用户并通过。
Now click on Generate script, do not apply changes. 现在单击生成脚本,不应用更改。
FINALLY: All the api goodness you wanted, including your sample code: 最后:您想要的所有api优点,包括示例代码:
Try this ( from How can I change the username/password of an ApplicationPool in IIS from C#? ) 尝试一下( 如何从C#中更改IIS中ApplicationPool的用户名/密码? )
myAppPool .Properties["AppPoolIdentityType"].Value = 3;
myAppPool .Properties["WAMUserName"].Value = Environment.MachineName + @"\" + username;
myAppPool .Properties["WAMUserPass"].Value = password;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.