O'Reilly logo

Professional Visual Basic 2012 and .NET 4.5 Programming by Todd Herman, Gastón Hillar, David McCarter, Rob Windsor, Billy Hollis, Bill Sheldon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required