Overview Security Classes in VB.NET: Part 2

In this article I will explain you about Security Classes in VB.NET.
  • 1297

See Part 1
The table 22.5 given below lists the most common methods used with the permission classes. Three of these methods-Assert, Deny, and PermitOnly-are called overrides because they override the default behavior of the security system. When different overrides are present in the same stack frame, the runtime processes these overrides in the following order: PermitOnly, Deny, and Assert

Table 22.5: Commonly Used Methods of Permissions Classes


If, during the stack walk, the runtime discovers more than one override of the same type (e.g., two calls to Assert in one stack frame), the second override causes an exception. To replace an override, first call the appropriate revert method (e.g., RevertAssert) and then apply the new override. Also, be aware that each stack frame can have, at most, one permission set used for denial. The most recent Deny function replaces all other denials for other permission sets in the current stack frame.

In Listing 22.15, a demand for a read operation to c:\dir1\ is called. If the system refuses that demand, an exception is thrown.

Listing 22.15: Executing the Demand method of a Permissions object

' Demand
            Dim p As New FileIOPermission(FileIOPermissionAccess.Read, "C:\dir1\")
            ' catch SecurityException here
        Catch ex As SecurityException

        End Try

The code in Listing 22.16 compares two file permissions to determine whether one is a subset of the other. If so, the subset may derive granted permissions from the "parent." For example, if file I/O permission to c:\dir1\ is granted, then file I/O permission to c:\dir1\dir2\ is also granted. Thus, you can conclude that perm2 is a subset of perm1 in this example.

Listing 22.16: IsSubsetOf Example

' IsSubsetOf tests
Imports System.Security
Imports System.Security.Permissions
Class TestClass
    Shared Sub Main()
            ' create two registry permissions
            Dim perm1 As New RegistryPermission(RegistryPermissionAccess.AllAccess,
            Dim perm2 As New RegistryPermission(RegistryPermissionAccess.AllAccess,
            ' and test which is subset of which?
            Console.WriteLine(If(perm1.IsSubsetOf(perm2), "perm1 is subset of perm2.", "test1 not
            ' The program will output:
            '            test1 not successful!
            '            perm2 is subset of perm1.
            Console.WriteLine(If(perm2.IsSubsetOf(perm1), "perm2 is subset of perm1.", "test2

        Catch e As Exception
            Console.WriteLine("SecurityException occurred! " & e.ToString())
        End Try
    End Sub
End Class

Output Window

securitycls 2.gif


Hope this article would have helped you in understanding Security Classes in VB.NET. The third part of this article you will in my next article.


More Articles

© 2013 dotNetheaven. All rights reserved.