You receive a "System.Security.SecurityException" exception when you run an application that calls the SystemInformation.UserName property (814741)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
SYMPTOMSWhen 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. CAUSEThe 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.RESOLUTIONTo 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.REFERENCESFor more information, visit the following MSDN Web sites:
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbUser kbSecurity kbWindowsForms kbbug KB814741 kbAudDeveloper |
---|
|