CSharpFeeds - All your C# feeds in one place.

Sponsors

Tuesday, October 24, 2006

Improved SecurityException Class in .NET 2.0

by Keyvan Nayyeri via Keyvan Nayyeri on 10/24/2006 4:30:07 AM

One of useful improvements in .NET was around SecurityException class.  Newly added properties are really helpful to work with security exceptions.

Previously this object had only four properties but now it comes with several properties (in addition to some properties that it inherits from SystemException) which are listed here:

  • Action (new): SecurityAction that caused this exception.
  • Demanded (new): Gets or sets the permissions, permission sets or permissions set collections which caused the error. 
  • DenySetInstance (new): Gets or sets the denied permissions, permission sets or permissions set collections which caused the error.
  • FailedAssemblyInfo (new): Returns information about failed assembly.
  • FirstPermissionThatFailed (new): Retrieves first permission in permissions set or permissions set collection which failed.
  • GrantedSet: Returns the granted permission set of the assembly that caused the error.
  • Method (new): Provides information about the method associated with exception.
  • PermissionState: Retrieves the state of the permission that caused the exception.
  • PermissionType: Retrieves the type of the permission that caused the exception.
  • PermitOnlySetInstance (new): Returns the permission, permission set or permission set collection that is a part of permit-only stack frame which caused a security check to fail.
  • RefusedSet: Gets or sets of refused permission set of assembly that caused the exception.
  • Url (new): Retrieves the Url of the assembly that caused the exception.
  • Zone (new): Returns the SecurityZone of the assembly that caused the exception.

For example following code throws a SecurityException under ASP.NET Medium Trust which is handled by a SecurityException:

private void ThrowSecurityException()

{

    String path = @"C:\Documents and Settings\Keyvan Nayyeri\My Documents\Visual Studio 2005\WebSites\SecurityExceptionExample\keyvan.txt";

    try

    {

        File.GetAccessControl(path);

    }

    catch (SecurityException ex)

    {

        Response.Write(ex.Action.ToString());

    }

}

Like many other debugging features in Visual Studio 2005, it provides great debugging support for SecurityExceptions and gives good details about them in order to help troubleshooting for this type of exceptions.

This code throws a SecurityException:

private void ThrowSecurityException()

{

    String path = @"C:\Documents and Settings\Keyvan Nayyeri\My Documents\Visual Studio 2005\WebSites\SecurityExceptionExample\keyvan.txt";

 

    File.GetAccessControl(path);     

}

Visual Studio provides this information for the exception that occurs:

SecurityException

email it!bookmark it!digg it!

Original Post: Improved SecurityException Class in .NET 2.0

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us