Why do I get a SecurityException when running from the network?

Hi,

  I 'm writing an EXE file using C#.  In my EXE, I'm trying to get access to Environment variables.  Everything is working fine locally.  I have the statement "Environment.GetEnvironmentVariable("TEMP")" which works on my local machine.

  As soon as I transfer the program to a networked drive, everytime I run the application it spits out the following error message:
--- BEGIN SNIP ---
Unhandled Exception: System.TypeInitializationException: The type initializer for "XVPinpal.validator" threw an
exception. ---> System.Security.SecurityException: Request for the permission of type System.Security.Permission
s.EnvironmentPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
   at System.Security.CodeAccessSecurityEngine.CheckHelper(PermissionSet grantedSet, PermissionSet deniedSet, Co
deAccessPermission demand, PermissionToken permToken)
   at System.Security.CodeAccessSecurityEngine.Check(PermissionToken permToken, CodeAccessPermission demand, Sta
ckCrawlMark& stackMark, Int32 checkFrames, Int32 unrestrictedOverride)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.Environment.GetEnvironmentVariable(String variable)
   at XVPinpal.validator.GetEnvironmentVariable(String variable)
   at XVPinpal.validator..cctor()

The state of the failed permission was:
<IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=1.0.5000.0, Culture=neu
tral, PublicKeyToken=b77a5c561934e089"
             version="1"
             Read="INCLUDE"/>

   --- End of inner exception stack trace ---
   at XVPinpal.validator.Main(String[] args)

--- END SNIP ---

Anybody that can shed some light on this   I 'm really confused.  I have tried using permission sets, and demanding permission on the environment, but I am at a loss.

Thanks



Answer this question

Why do I get a SecurityException when running from the network?

  • JelloMiket

    This is with .Net Fremework 1.1 but I have to do it with .Net Framework 2.0.

    I remember in the .Net Configuration Framework 1.1, I did it in My Computer\Runtime Security Policy\Machine\Code Groups\All_Code and right click New. I do the same in .Net Framework 2.0 but It does not work.

    Thanks.


  • Brass Ring

    I have Microsoft .NET Framework 2.0, I can't follow any wizard, because doesn't exist, what can I do to resolve my problem
  • Lapostal

    Roberto,

    As we speak I'm chasing up a link for the Active Directory deployment, hopefully I should find it in the next couple of days.

    You can't do it for a entire network share, however, if you sign all your assemblies via the same strong name key, you only have to trust that.

    This security scheme is actually quite powerful as it allows you permit certain code to only call safe functions. This enables things such as hosting .NET controls within Internet Explorer, or the new ClickOnce deployment model.

    Regards

    David



  • mul2006

    Roberto,

    To do this, instead of choosing the This one assembly option for step 5, choose the All assemblies with the same assembly public key option instead.

    Regards

    David



  • koad

    Ahhhh.  I thought it had something to do with machine settings.  I just wasn't sure what I could do about it.  I will try this and let you know if I have any more questions.

    Thanks!

  • joeslide

    Can this be done programatically

  • EdenR

    Can you give me a link on how to do this work with Active Directory in .Net Framework 2.0

    Can I do it to a complete network share directory and not for each application

    I don't understand the reason of this security scheme


  • MHS-TEAM

    Confused,

    It's not enough just to demand the permissions, you need to give your application the required permissions it needs.

    By default, applications run via the network are not given as much permissions as applications run on the local machine.

    To give your application permissions to run, do the following on the machine that is running the application:

    1. Choose Start -> Control Panel -> Administrative Tools -> Microsoft .NET Framework 1.1 Wizards
    2. Choose Trust an Assembly
    3. Choose whether you want to make changes for the computer or current user and click Next
    4. Enter the UNC location of the application you want to run and click Next
    5. Click Next to trust just this assembly
    6. Adjust the slider to Full Trust and click Next
    7. Click Finish

    You application will now have the trust it requires to run from the local network. Please note that you will need to do this for each PC the application is running on.  There are ways to automatically do this via Active Directory, however this beyond the scope of this post.

    HTH

    David

  • jradi

    I have created a strong name key file, and I will sign all the assembly’s with the same key file. Now, how can I give full trust to my assembly’s using the .Net Framework 2.0 Configuration tool Or using the command-line tool caspol.exe

    Thanks.


  • Clemenza

    You need to use the command-line tool called caspol.exe, which is available in the .NET Directory (typically c:\Windows\Microsoft.NET\Framework\[Version]).

  • Why do I get a SecurityException when running from the network?