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

Article translations Article translations
Article ID: 814741 - View products that this article applies to.
Expand all | Collapse all

On This Page

SYMPTOMS

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.

CAUSE

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.

RESOLUTION

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 
            Console.WriteLine(System.Environment.UserName)
            Console.ReadLine()
    Visual C# .NET Code
    //display the UserName currently logged
    			Console.WriteLine(System.Environment .UserName);
    			Console.ReadLine();
  2. On the Debug menu, click Start.

STATUS

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 Select.
  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 
    Console.WriteLine(SystemInformation.UserName)
    Visual C# .NET Code
    //display the UserName that is currently logged
    Console.WriteLine(SystemInformation.UserName);
  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 Machine.
  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 Open.
  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.

REFERENCES

For more information, visit the following MSDN Web sites:
SystemInformation.UserName Property
http://msdn2.microsoft.com/en-us/library/system.windows.forms.systeminformation.username(vs.71).aspx
Environment.UserName Property
http://msdn2.microsoft.com/en-us/library/system.environment.username(vs.71).aspx

Properties

Article ID: 814741 - Last Review: November 14, 2007 - Revision: 3.6
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbuser kbsecurity kbwindowsforms kbbug KB814741

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com