Environment.Machinename secutiry exception

T

tmoffett

I am very new to the language so please pardon my ignorance.

I am writing a small console application to gather hardware inventory
information. When I run the application locally all works well. It
gathers the needed information and writes that info to a remote SQL
server.

If I put the file in the scripts folder on our AD domain controller (so
that I can use it with login scripts) it fails to run. It also fails
when I run it from any network share. I am a domain admin and I have
admin rights on each of the PC's I've tried to run it on as well as
Full Control of the share and NTFS permissions. After debugging I find
the exception was thrown on this line of code.

myMachineName = Environment.MachineName.ToLower();

The exception is this:

System.Security.SecurityException was unhandled
Message="Request for the permission of type
'System.Security.Permissions.EnvironmentPermission, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed."
Source="mscorlib"
StackTrace:
at System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Environment.get_MachineName()
at ITInventory.Program.Main(String[] args)

Any help you can offer would be greatly appreciated.
 
N

Nicholas Paldino [.NET/C# MVP]

tmoffett,

The problem is the result of the location that the assembly is being
loaded from. By default, assemblies not loaded from the local machine run
with a reduced permission set.

In order to get around this, you would have to go to the .NET
configuration wizard and create a permission set which would grant you the
Environment permission for your assembly (best indicated with a strong name,
or at worst, a hash).

Hope this helps.
 
A

Alan Pretre

tmoffett said:
If I put the file in the scripts folder on our AD domain controller (so
that I can use it with login scripts) it fails to run. It also fails
when I run it from any network share.

You may need to adjust your Local Intranet zone security to Full Trust. You
can find this in the .NET Framework x.x Configuration utility, which should
be in your Administrative Tools folder on the computer of interest.

-- Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top