访问被拒绝从远程计算机(Asp.Net)读取Perfmon计数器 [英] Access denied reading Perfmon counters from a remote machine (Asp.Net)

查看:106
本文介绍了访问被拒绝从远程计算机(Asp.Net)读取Perfmon计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的Asp.Net页面,以从远程计算机读取Perfmon计数器.

I'm trying to create a simple Asp.Net page to read Perfmon counters from a remote machine.

当我使用Visual Studio开发Web服务器运行页面时,一切都很好,但是当我尝试将同一页面托管在IIS上时运行同一页面时,在实例化Perfmon计数器的行上出现了拒绝访问错误:

When I run the page using the Visual Studio development web server all is well, however when I attempt to run the same page when its hosted on IIS I get an access denied error on the line that instantiates the Perfmon counter:

PerformanceCounter freeSpaceCounter = new PerformanceCounter("LogicalDisk", "Free Megabytes", "D:", "RemoteMachine12");

这是我得到的例外:

Exception Details: System.ComponentModel.Win32Exception: Access is denied

我尝试同时使用匿名访问(以我自己作为匿名用户)和集成Windows身份验证-都无法正常工作.显然,其他一些帐户也用于读取PerfMon计数器(例如ASPNET帐户).如何使我的页面使用我的帐户而不是该帐户访问PerfMon计数器?

I've tried using both anonymous access (with myself as the anonymous user) and Integrated Windows Authentication - both dont work. Clearly some other account is be used to read the PerfMon counters (like the ASPNET account). How do I get my page to access the PerfMon counters using my account rather than that account?

推荐答案

您遇到的问题是IIS在本地帐户的上下文中运行(默认情况下).该本地帐户在远程计算机上不存在,因此无法连接以获取性能计数器.当您使用VS开发Web服务器时,它在您自己的本地帐户下运行,因此一切正常.

The problem you have here is that IIS runs under the context of a local account (by default). This local account doesn't exist on the remote machine, and so can't connect to get the performance counters. When you use the VS development web server it runs under your own local account and so everything works.

如果您在域环境中,则可以将IIS应用程序池配置为可以同时访问两台计算机的域帐户运行,并且一切正常,但是您可能希望对此有更多控制权.

If you're in a domain environment you can configure the IIS application pool to run as a domain account with access to both machines, and everything will work, however you may want more control over this.

您可以使用基本身份验证,将应用程序配置为模拟(或者,如果您使用的是IIS7,其池配置为在经过身份验证的帐户下运行),或者在读取计数器之前进行模拟.

You could either use basic authentication, with the application configured for impersonation (or if you're using IIS7 having the pool configured to run under the authenticated account) or you impersonate just before you read the counter.

有两种方法可以模拟-最安全的方法是将IIS配置为使用集成身份验证,然后包装呼叫

There are a couple of ways to impersonate - the safest is to configure IIS to use integrated authentication and then wrap the call up

PerformanceCounter freeSpaceCounter = null;
using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
{
    freeSpaceCounter = new PerformanceCounter("LogicalDisk", 
                               "Free Megabytes", "D:", "RemoteMachine12");
}

如果您不想进行身份验证,则必须配置应用程序池,或在应用程序中对用户名和密码进行硬编码-这应该是最后的选择,请参见

If you don't want authentication then you'll have to configure the app pool, or hard code a username and password in your application - this should be the last resort, see KB306158

这篇关于访问被拒绝从远程计算机(Asp.Net)读取Perfmon计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆