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

发布于 2024-08-30 15:46:58 字数 1267 浏览 10 评论 0原文

我需要从同一域中的另一台计算机监视 IIS 7 应用程序池中应用程序的状态。我的监控应用程序必须采用 C# 语言并作为 Windows 服务运行。

在我的服务器上,我创建了一个具有管理权限的用户,并执行命令 aspnet_regiis -ga machine\username ,该命令成功运行。

我的问题是,当我尝试访问应用程序池时,我仍然收到 COMException“访问被拒绝”。 我做错了什么或者我错过了哪一步?

我使用了 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)
        {

        }

I need to monitor the status of an application in the applications pool of IIS 7 from another machine on the same domain. My monitoring application must be in C# and running as a Windows service.

On my server, I create a user with administration rights and I execute the command aspnet_regiis -ga machine\username which worked successfully.

My problem is when I try to access the application pool I still get COMExcepttion "Access denied".
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.

        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)
        {

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

献世佛 2024-09-06 15:46:58

您找到的代码似乎适用于 IIS6。也许使用新的受支持的 IIS7 管理 API 会更好。您可以首先调用 ServerManager.OpenRemote 获取 ServerManager 对象。

The code you found seems to be for IIS6. Perhaps you will be better off using the new and supported IIS7 management API. You could start by calling ServerManager.OpenRemote to get the ServerManager object.

多情出卖 2024-09-06 15:46:58

您可能需要搞乱 AuthenticationType,从 2.0 开始默认是安全的,但您可能需要设置 SSL。此外,我还看到来自帐户的“访问被拒绝”消息,其中选中了“用户下次登录时必须更改密码”。

You might need to mess around with the AuthenticationType, the default starting with 2.0 is Secure but you might need to set SSL. Also, I've seen Access Denied messages from accounts with the "user must change password on next logon" checked.

小姐丶请自重 2024-09-06 15:46:58

这在 Windows 7 和 Windows Server 2008 上运行得很好(不幸的是在 XP 和 2003 Server 上不行)。我必须通过服务器管理器在 IIS 中添加管理服务角色才能启用远程连接。

以下是如何获取应用程序池状态的简短示例。

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;
        }
    }

感谢德里斯。

This works pretty well on Windows 7 and Windows server 2008 (unfortunately not on XP and 2003 Server). I had to add Management Service role in the IIS via the Server Manager in order to enable remote connection.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文