473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SecurityException with PerformanceCounter on remote server in .NET 2.0

I have been struggling with a security issue that occurs under .NET
2.0, but does not occur under .NET 1.1. Essentially I am trying to
open up a performance counter on a remote server and monitor its value.
In .NET 1.1 this worked fine, however under .NET 2.0 it fails when I
am not an administrator on the remote server.

To provide a lean demonstration of the issue, I created the following
class:

============================

using System;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication3
{
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
string machineName = null;

// Was exactly 1 argument was passed?
if (args.Length != 1)
{
// Prompt for machine name
Console.Write("Please enter a machine name: ");
machineName = Console.ReadLine();
}
else
{
// Use command line argument
machineName = args[0];
}

// Get the performance counter
PerformanceCounter c = new PerformanceCounter("Processor", "%
Processor Time", "_Total", machineName);

// Display counter value once every second for 5 seconds
for (int i = 0; i < 5; i++)
{
Console.WriteLine(c.RawValue);
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine("==============");
Console.WriteLine(ex);
Console.WriteLine("==============");
}
finally
{
Console.WriteLine();
Console.WriteLine("Hit ENTER to quit application.");
Console.ReadLine();
}
}
}
}

============================

I then created a batch file that compiles this file under both .NET 1.1
and .NET 2.0:

c:\windows\Microsoft.NET\Framework\v1.1.4322\csc.e xe
/out:CounterTest11.exe /t:exe Class1.cs
c:\windows\Microsoft.NET\Framework\v2.0.50727\csc. exe
/out:CounterTest20.exe /t:exe Class1.cs
Now, when I run either version against a server on which I am an
administrator, I get the following output:
--------
C:\Projects\ConsoleApplication3>CounterTest20.exe
Please enter a machine name: speck
6708935078125
6708945000000
6708954921875
6708964609375
6708974687500

Hit ENTER to quit application.
--------

When I run the .NET 1.1 version against a server on which I am only a
member of the "Performance Monitoring" group, I get the following
results:
--------
C:\Projects\ConsoleApplication3>CounterTest11.exe
Please enter a machine name: bonnie
114549931445312
114549938945312
114549946445312
114549954023437
114549961523437

Hit ENTER to quit application.
--------

But when I run the .NET 2.0 version against the same server, I get the
following:
--------
C:\Projects\ConsoleApplication3>CounterTest20.exe
Please enter a machine name: bonnie
==============
System.Security.SecurityException: Requested registry access is not
allowed.
at System.ThrowHelper.ThrowSecurityException(Exceptio nResource
resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean
writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at
System.Diagnostics.PerformanceCounterLib.FindCusto mCategory(String
categor
y, PerformanceCounterCategoryType& categoryType)
at System.Diagnostics.PerformanceCounterLib.GetCatego ryType(String
machine, S
tring category)
at System.Diagnostics.PerformanceCounter.Initialize()
at System.Diagnostics.PerformanceCounter..ctor(String categoryName,
String co
unterName, String instanceName, String machineName)
at ConsoleApplication3.Class1.Main(String[] args)
The Zone of the assembly that failed was:
MyComputer
==============

Hit ENTER to quit application.

Now, I posted this earlier to another forum but the only suggestion was
that I was not running with Full Trust. I am launching the application
locally, and so I don't understand how I could not be running under
Full Trust. I poked around in the .NET 2.0 configuration tool, but
could not come up with any ideas.

Any insight into this problem would be very much appreciated!
Geoff McElhanon

Aug 24 '06 #1
3 4562
V2 uses WMI to collect performance information from the remote perfmon
(through the remote registry), that means that the impersonated user
(remote) must be a member of the (remote)administrators alias, only
administrators are allowed to read perf data.

Willy.

"Geoff McElhanon" <gm********@yahoo.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
|I have been struggling with a security issue that occurs under .NET
| 2.0, but does not occur under .NET 1.1. Essentially I am trying to
| open up a performance counter on a remote server and monitor its value.
| In .NET 1.1 this worked fine, however under .NET 2.0 it fails when I
| am not an administrator on the remote server.
|
| To provide a lean demonstration of the issue, I created the following
| class:
|
| ============================
|
| using System;
| using System.Diagnostics;
| using System.Threading;
|
| namespace ConsoleApplication3
| {
| class Class1
| {
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
| [STAThread]
| static void Main(string[] args)
| {
| try
| {
| string machineName = null;
|
| // Was exactly 1 argument was passed?
| if (args.Length != 1)
| {
| // Prompt for machine name
| Console.Write("Please enter a machine name: ");
| machineName = Console.ReadLine();
| }
| else
| {
| // Use command line argument
| machineName = args[0];
| }
|
| // Get the performance counter
| PerformanceCounter c = new PerformanceCounter("Processor", "%
| Processor Time", "_Total", machineName);
|
| // Display counter value once every second for 5 seconds
| for (int i = 0; i < 5; i++)
| {
| Console.WriteLine(c.RawValue);
| Thread.Sleep(1000);
| }
| }
| catch (Exception ex)
| {
| Console.WriteLine("==============");
| Console.WriteLine(ex);
| Console.WriteLine("==============");
| }
| finally
| {
| Console.WriteLine();
| Console.WriteLine("Hit ENTER to quit application.");
| Console.ReadLine();
| }
| }
| }
| }
|
| ============================
|
| I then created a batch file that compiles this file under both .NET 1.1
| and .NET 2.0:
|
| c:\windows\Microsoft.NET\Framework\v1.1.4322\csc.e xe
| /out:CounterTest11.exe /t:exe Class1.cs
| c:\windows\Microsoft.NET\Framework\v2.0.50727\csc. exe
| /out:CounterTest20.exe /t:exe Class1.cs
|
|
| Now, when I run either version against a server on which I am an
| administrator, I get the following output:
| --------
| C:\Projects\ConsoleApplication3>CounterTest20.exe
| Please enter a machine name: speck
| 6708935078125
| 6708945000000
| 6708954921875
| 6708964609375
| 6708974687500
|
| Hit ENTER to quit application.
| --------
|
| When I run the .NET 1.1 version against a server on which I am only a
| member of the "Performance Monitoring" group, I get the following
| results:
| --------
| C:\Projects\ConsoleApplication3>CounterTest11.exe
| Please enter a machine name: bonnie
| 114549931445312
| 114549938945312
| 114549946445312
| 114549954023437
| 114549961523437
|
| Hit ENTER to quit application.
| --------
|
| But when I run the .NET 2.0 version against the same server, I get the
| following:
| --------
| C:\Projects\ConsoleApplication3>CounterTest20.exe
| Please enter a machine name: bonnie
| ==============
| System.Security.SecurityException: Requested registry access is not
| allowed.
| at System.ThrowHelper.ThrowSecurityException(Exceptio nResource
| resource)
| at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean
| writable)
| at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
| at
| System.Diagnostics.PerformanceCounterLib.FindCusto mCategory(String
| categor
| y, PerformanceCounterCategoryType& categoryType)
| at System.Diagnostics.PerformanceCounterLib.GetCatego ryType(String
| machine, S
| tring category)
| at System.Diagnostics.PerformanceCounter.Initialize()
| at System.Diagnostics.PerformanceCounter..ctor(String categoryName,
| String co
| unterName, String instanceName, String machineName)
| at ConsoleApplication3.Class1.Main(String[] args)
| The Zone of the assembly that failed was:
| MyComputer
| ==============
|
| Hit ENTER to quit application.
|
| Now, I posted this earlier to another forum but the only suggestion was
| that I was not running with Full Trust. I am launching the application
| locally, and so I don't understand how I could not be running under
| Full Trust. I poked around in the .NET 2.0 configuration tool, but
| could not come up with any ideas.
|
| Any insight into this problem would be very much appreciated!
|
|
| Geoff McElhanon
|
Aug 24 '06 #2
Thank you, oh bearer of bad news. :-(

But seriously... thank you.

Willy Denoyette [MVP] wrote:
V2 uses WMI to collect performance information from the remote perfmon
(through the remote registry), that means that the impersonated user
(remote) must be a member of the (remote)administrators alias, only
administrators are allowed to read perf data.

Willy.
Aug 24 '06 #3
I would never use the PerformanceCounter class to access remote systems when
not running in a domain realm, much better is to use System.Management
clases (WMI wrappers) to access management objects like performance
counters. Note that even in this case you need to enable remote access to
the WMI service namespace.
Willy.
"Geoff McElhanon" <gm********@yahoo.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
| Thank you, oh bearer of bad news. :-(
|
| But seriously... thank you.
|
| Willy Denoyette [MVP] wrote:
| V2 uses WMI to collect performance information from the remote perfmon
| (through the remote registry), that means that the impersonated user
| (remote) must be a member of the (remote)administrators alias, only
| administrators are allowed to read perf data.
| >
| Willy.
| >
|
Aug 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sam | last post by:
I have a desktop VB.Net application I developed locally. I uploaded it to a file server we have and the .Net Framework is installed on the file server. When I try to run the executable from the...
1
by: W1ld0ne74 | last post by:
I am implimenting Performance counters into a web application. I use the following code to create the counters during setup: private void SetupPerfCntrs() {...
1
by: jimbo | last post by:
Here is my problem. I'm creating an Instrumentation class that will use previously created Performance Categories and Counters in order to time various processes (ie. query duration etc.). This...
2
by: jwgoerlich | last post by:
I wrote a simple VB.Net console application. It accepts command line parameters and creates a folder structure. It works on the local file system without a problem If I run the console app from...
3
by: Rob Meade | last post by:
Hi all, I'm having a bit of trouble with the following function.... Private Function GetSystemUpTime() As TimeSpan ' declare variables Dim Result As TimeSpan Dim PerformanceCounter As...
0
by: gmcelhanon | last post by:
I have some code that attempts to read a performance counter on a remote machine. Under .NET 1.1 the code works just fine, but under ..NET 2.0 I get a SecurityException with a description of...
4
by: =?Utf-8?B?c2FtMDFt?= | last post by:
Not that I've ever gotten any reponse to questions in the past, but I don't know where else to turn. I have a remoting application (.NET 2.0, VS 2005, C#) with a custom ChannelSinkProvider as...
11
by: =?Utf-8?B?U2FsYW1FbGlhcw==?= | last post by:
Has anybody worked with performancecounter object to access counters on remote machine? I managed to write code that retrieves counters categories froma remote machine, when I try another remote...
5
by: Henry Stock | last post by:
I am trying to understand the following error: Any thing you can tell me about this is appreciated. Security Exception Description: The application attempted to perform an operation not allowed...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.