简体   繁体   English

使用 csharp 检查状态应用程序池 iis7(拒绝访问)

[英]check status application pool iis7 with csharp (access-denied)

I need to monitor the status of an application in the applications pool of IIS 7 from another machine on the same domain.我需要从同一域中的另一台机器监视 IIS 7 应用程序池中的应用程序状态。 My monitoring application must be in C# and running as a Windows service.我的监控应用程序必须使用 C# 并作为 Windows 服务运行。

On my server, I create a user with administration rights and I execute the command aspnet_regiis -ga machine\\username which worked successfully.在我的服务器上,我创建了一个具有管理权限的用户,并执行成功运行的命令aspnet_regiis -ga machine\\username

My problem is when I try to access the application pool I still get COMExcepttion "Access denied".我的问题是当我尝试访问应用程序池时,我仍然收到 COMExcepttion“拒绝访问”。 What did I do wrong or which step did I miss?我做错了什么或者我错过了哪一步?

I used code from http://patelshailesh.com/index.php/create-a-website-application-pool-programmatically-using-csharp as example.我使用来自http://patelshailesh.com/index.php/create-a-website-application-pool-programmatically-using-csharp 的代码作为示例。

        int status = 0;
        string ipAddress = "10.20.2.13";
        string username = "username";
        string password = "password";
        try
        {
            DirectoryEntry de = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools/MyAppPoolName", ipAddress), username, password);

            //the exception is thrown here.
            status = (int)de.InvokeGet("AppPoolState");

            switch (status)
            {
                case 2:
                    //Running
                    break;
                case 4:
                    //Stopped
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {

        }

The code you found seems to be for IIS6.您找到的代码似乎适用于 IIS6。 Perhaps you will be better off using the new and supported IIS7 management API.也许您最好使用新的受支持的 IIS7 管理 API。 You could start by calling ServerManager.OpenRemote to get the ServerManager object.您可以首先调用ServerManager.OpenRemote来获取ServerManager对象。

You might need to mess around with the AuthenticationType , the default starting with 2.0 is Secure but you might need to set SSL.您可能需要处理AuthenticationType ,从 2.0 开始的默认值是 Secure 但您可能需要设置 SSL。 Also, I've seen Access Denied messages from accounts with the "user must change password on next logon" checked.此外,我已经看到来自帐户的访问被拒绝消息,其中“用户必须在下次登录时更改密码”被选中。

This works pretty well on Windows 7 and Windows server 2008 (unfortunately not on XP and 2003 Server).这在 Windows 7 和 Windows Server 2008 上运行良好(不幸的是不适用于 XP 和 2003 Server)。 I had to add Management Service role in the IIS via the Server Manager in order to enable remote connection.我必须通过服务器管理器在 IIS 中添加管理服务角色才能启用远程连接。

Here's an short example of how to get the State of an Application Pool.这是一个关于如何获取应用程序池状态的简短示例。

public ObjectState State
    {
        get
        {
            ServerManager server = null;
            ObjectState result = ObjectState.Unknown;
            try
            {
                server = ServerManager.OpenRemote(address);
                result = server.ApplicationPools[name].State;
            }
            finally
            {
                if (server != null)
                    server.Dispose();
            }

            return result;
        }
    }

Thanks to driis.感谢德里斯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM