Managing Code Access Permission Sets

This section looks at programmatic access to permissions. The example extends the ProVB2012_Security project discussed earlier. This example illustrates how when a method fails, an exception object containing the result is generated. Note that in the case of a real-world example, you would be setting up permissions for a calling application. In many instances, you don't want a calling application to be able to access the registry, or you want a calling application to be able to read memory variables but not change them. Keep in mind that you can only limit those permissions which are already available to a user based on their identity. You can't grant access to a portion of the operating system via code that the user doesn't have access to based on their identity.

The following example first sets up the permission that is wanted and then grants the code the appropriate access level. Thee code that accesses this security object illustrates the effect of these new permissions on the code. Note you will need to add the line Imports System.IO to the top of your MainWindow.xaml.vb file for the following code to compile.

 Private Sub TestFileIOPermission() Dim oFp = New FileIOPermission( FileIOPermissionAccess.AllAccess, "C:\Temp") oFp.PermitOnly() 'Try Dim strmWrite As New IO.StreamWriter( File.Open("C:\Temp\Permission.txt", IO.FileMode.Open)) strmWrite.WriteLine("Hi there!") strmWrite.Flush() strmWrite.Close() Dim objWriter As New IO.StreamWriter( ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.