Microsoft KB Archive/814741

= You receive a &quot;System.Security.SecurityException&quot; exception when you run an application that calls the SystemInformation.UserName property =

Article ID: 814741

Article Last Modified on 11/14/2007

-

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

-



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:   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;  On the Debug menu, click Start.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 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#. In Solution Explorer, right-click ConsoleApplication1, and then click Add Reference. On the .NET tab, click to select System.Windows.Forms.dll, and then click Select.</li>  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; </li>  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); </li> 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.</li>  Run the following statement at the command prompt: sn -k mykey.snk </li> In the ConsoleApplication1 Solution Explorer, double-click AssemblyInfo.vb.</li>  Add the following statement to the code:

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

Replace the existing statement [assembly: AssemblyKeyFile(&quot;&quot;)] with the following statement: [assembly: AssemblyKeyFile(&quot;local_file_path/mykey.snk&quot;)] Note Replace  with the path of Mykey.snk on your local computer. </li> On the Build menu, click Build Solution.</li> Click Start, point to Administrative Tools, and then click Microsoft .NET Framework Configuration.</li> In the .NET Framework Configuration window, expand Runtime Security Policy, and then expand Machine.</li> In the Machine window, expand Code Groups, and then expand All_Code.</li> Right-click LocalIntranet_Zone, and then click Properties.</li> 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.</li> Click the Membership Condition tab. Under Choose the condition type for this code group, click to select Strong Name.</li> Click Import, locate the ConsoleApplication1.exe file, and then click to select the file. Click Open.</li> Click OK to apply the assigned properties.</li> <li>In ConsoleApplication1, click Start on the Debug menu.

You receive the exception mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">