G
Geoff McElhanon
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.exe
/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(ExceptionResource
resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean
writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at
System.Diagnostics.PerformanceCounterLib.FindCustomCategory(String
categor
y, PerformanceCounterCategoryType& categoryType)
at System.Diagnostics.PerformanceCounterLib.GetCategoryType(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
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.exe
/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(ExceptionResource
resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean
writable)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name)
at
System.Diagnostics.PerformanceCounterLib.FindCustomCategory(String
categor
y, PerformanceCounterCategoryType& categoryType)
at System.Diagnostics.PerformanceCounterLib.GetCategoryType(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