phrackattack
May 1st, 2007, 05:36 PM
Hi guys, I'm having a spot of trouble using WMI. When I try to run any code that makes use of WMI (and actually, I suspect anything that calls System.Security.SecurityException) I get this error:
Unhandled Exception: System.Security.SecurityException: That assembly does not allow partially trusted callers.
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at Test_WMI2.Program.Main(String[] args)
The action that failed was:
LinkDemand
The assembly or AppDomain that failed was:
Test WMI2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
The Zone of the assembly that failed was:
Intranet
The Url of the assembly that failed was:
file:///P:/My Documents/Visual Studio 2005/Projects/Test WMI2/Test WMI2/bin/Debug/Test WMI2.exe
Everything compiles fine, of course. This happens with absolutely everything that uses WMI. Here's some sample C# code that can be used to generate this error:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace Test_WMI2
{
class Program
{
static void Main(string[] args)
{
// Create a query for system environment variables only
SelectQuery query =
new SelectQuery("Win32_Environment", "UserName=\"<SYSTEM>\"");
// Initialize an object searcher with this query
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
// Get the resulting collection and loop through it
foreach (ManagementBaseObject envVar in searcher.Get())
{
Console.WriteLine("System environment variable {0} = {1}",
envVar["Name"], envVar["VariableValue"]);
}
}
}
}
Pulled straight from the MSDN library, this should work with no issues. I'm guessing this is a system configuration issue or something, but there's not much I can do about that considering this is my company's machine, and not mine. The program that I'm trying to write that uses WMI, however, has to run on these computers, which are running .NET 2.
Unhandled Exception: System.Security.SecurityException: That assembly does not allow partially trusted callers.
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at Test_WMI2.Program.Main(String[] args)
The action that failed was:
LinkDemand
The assembly or AppDomain that failed was:
Test WMI2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
The Zone of the assembly that failed was:
Intranet
The Url of the assembly that failed was:
file:///P:/My Documents/Visual Studio 2005/Projects/Test WMI2/Test WMI2/bin/Debug/Test WMI2.exe
Everything compiles fine, of course. This happens with absolutely everything that uses WMI. Here's some sample C# code that can be used to generate this error:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace Test_WMI2
{
class Program
{
static void Main(string[] args)
{
// Create a query for system environment variables only
SelectQuery query =
new SelectQuery("Win32_Environment", "UserName=\"<SYSTEM>\"");
// Initialize an object searcher with this query
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
// Get the resulting collection and loop through it
foreach (ManagementBaseObject envVar in searcher.Get())
{
Console.WriteLine("System environment variable {0} = {1}",
envVar["Name"], envVar["VariableValue"]);
}
}
}
}
Pulled straight from the MSDN library, this should work with no issues. I'm guessing this is a system configuration issue or something, but there's not much I can do about that considering this is my company's machine, and not mine. The program that I'm trying to write that uses WMI, however, has to run on these computers, which are running .NET 2.