简体   繁体   English

如何在IIS 8上以编程方式设置应用程序池标识

[英]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.

相关问题 以编程方式将应用程序池标识设置为域用户 - Programmatically set App Pool identity to Domain user 在IIS 7中以编程方式为自定义身份应用程序池设置用户帐户 - Programmatically set up user account for custom identity application pool in IIS 7 如何正确设置IIS 7应用程序池标识? - How to set up IIS 7 application pool identity correctly? 以编程方式将IIS应用程序池标识“用户”分配给组 - Programmatically assigning IIS Application Pool Identity “users” to Groups 如何将IIS应用程序池标识用户区域设置为ApplicationPoolIdentity时,如何设置它 - How do you set the IIS Application Pool Identity User Locale when it's set to ApplicationPoolIdentity IIS7以编程方式在ASP.NET中获取应用程序池版本 - IIS7 Programmatically get App Pool version in ASP.NET 如何使HttpClient使用应用程序池标识 - How to make HttpClient to use the app pool identity 以编程方式获取应用程序池标识 - Get the Application Pool Identity programmatically 如何以编程方式设置应用程序池空闲超时? - how to set application pool idle timeout programmatically? 在 ASP.Net Core 集成测试中模拟 IIS App Pool Identity - Simulating IIS App Pool Identity in ASP.Net Core Integration Test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM