You receive a "System.Security.SecurityException" exception when you run an application that calls the SystemInformation.UserName property


When you run an application that calls the SystemInformation.UserName property of the System.Windows.Forms assembly, and the application has Local Intranet zone permissions, you receive the following exception:
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll.

Additional information: Request for the permission of type System.Security.Permissions.EnvironmentPermission, mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.


The System.Security.Permissions.EnvironmentPermission class in Mscorlib.dll controls the access to the user environment variables. SystemInformation.UserName in the application requests the permission from the EnvironmentPermission class to access the UserName environment variable. However, in the Local Intranet zone, System.Windows.Forms does not have permissions to access the Windows user name, and the request is not served by Mscorlib.dll. Therefore, a security exception is raised when you run the application.


To resolve the problem, replace the SystemInformation.UserName property with System.Environment.UserName. To do this, follow these steps:
  1. In the Main procedure, replace the existing code with the following code:

    Visual Basic .NET or Visual Basic 2005 Code
    'display the UserName currently logged 
    Visual C# .NET Code
    //display the UserName currently logged
    Console.WriteLine(System.Environment .UserName);
  2. On the Debug menu, click


This behavior is by design.

More Information

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, start a new console application by using Microsoft Visual Basic or Microsoft Visual C#.
  2. In Solution Explorer, right-click
    ConsoleApplication1, and then click Add Reference.
  3. On the .NET tab, click to select
    System.Windows.Forms.dll, and then click
  4. In the code editor, add the following statement as the first line of code:

    Visual Basic .NET or Visual Basic 2005 Code
    Imports System.Windows.Forms
    Visual C# .NET Code
    using System.Windows.Forms;
  5. Add the following code to the Main procedure:

    Visual Basic .NET or Visual Basic 2005 Code
    'display the UserName that is currently logged 
    Visual C# .NET Code
    //display the UserName that is currently logged
  6. Click Start, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.

    Note In Visual Studio 2005, click Start, point to Microsoft Visual Studio 2005, point to Visual Studio 2005 Tools, and then click Visual Studio 2005 Command Prompt.
  7. Run the following statement at the command prompt:
    sn -k mykey.snk
  8. In the ConsoleApplication1 Solution Explorer, double-click AssemblyInfo.vb.
  9. Add the following statement to the code:

    Visual Basic .NET or Visual Basic 2005
    <Assembly: AssemblyKeyFile("local_file_path\mykey.snk")>
    Visual C# .NET

    Replace the existing statement
    [assembly: AssemblyKeyFile("")]
    with the following statement:
    [assembly: AssemblyKeyFile("local_file_path/mykey.snk")]
    Note Replace local_file_path with the path of Mykey.snk on your local computer.
  10. On the Build menu, click Build Solution.
  11. Click Start, point to
    Administrative Tools, and then click Microsoft .NET Framework Configuration.
  12. In the .NET Framework Configuration window, expand
    Runtime Security Policy, and then expand
  13. In the Machine window, expand Code Groups, and then expand All_Code.
  14. Right-click LocalIntranet_Zone, and then click Properties.
  15. On the General tab, click to select
    This policy level will only have the permissions from the permission set associated with this code group check box.
  16. Click the Membership Condition tab. Under
    Choose the condition type for this code group, click to select
    Strong Name.
  17. Click Import, locate the ConsoleApplication1.exe file, and then click to select the file. Click
  18. Click OK to apply the assigned properties.
  19. In ConsoleApplication1, click
    Start on the Debug menu.

    You receive the exception mentioned in the "Symptoms" section of this article.


For more information, visit the following MSDN Web sites:

Article ID: 814741 - Last Review: Apr 29, 2008 - Revision: 1