Overview Security Classes in VB.NET: Part 3
In this article I will explain you about Security Classes in VB.NET.
See Part 1, Part 2
The example22.17 given below illustrates a typical use of the Assert method. First, file I/O permission is demanded. Then the Assert method is called to affirm permission to unmanaged code. As a result, no more stack walk-ups are performed until processing of the unmanaged code is complete.
Listing 22.17: Assert Example
Dim p1 As New FileIOPermission(FileIOPermissionAccess.Read, "C:\dir1\")
Dim unmanagedPerm As New SecurityPermission(SecurityPermissionFlags.UnmanagedCode)
' call unmanaged code
' demand for file I/O permission failed.
Listing 22.18 uses the Demand and Assert methods together for performance tuning. First, you demand an environment variable for read permission. Then you assert that need and execute nonrisky code in terms of that environment permission.
Listing 22.18: Demand and Assert Example
' demand and assert together
Dim envPerm As New EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP")
' Demand it once to see if it has been granted.
' Assert the permission to stop the stack walk here.
' code that reads TEMP environment variable
For i As Integer = 0 To 99
' The demand failed.
SecurityAction is an enumeration that encompasses the following elements:
The SecurityPermissionFlag enumeration helps to specify access flags for the security permission object. The SecurityPermission class uses this enumeration. Many of these flags are powerful and should be granted only to highly trusted code.
The enumeration contains the following elements:
Listing 22.19 shows how to use the SecurityPermissionFlag enumeration to request minimum security in the class attributes. This causes security verification to be skipped during JIT compilation.
Listing 22.19: SecurityPermissionFlag
Public Class MySecureClass
Shared Sub DenyAllSecurityPermissions()
// code here
Hope this article would have helped you in understanding Security Classes in VB.NET.