Summary

Team Fly 

Page 136

        ' But remember and beware that any Shared
        ' methods in this class can bypass instantiation
        ' so in the following case, if you must use
        ' Shared, you have to repeat the permission test:
        Public Shared Sub ReadAFile()
            Dim p As New FileIOPermission(PermissionState.Unrestricted)
            p.Demand()
        End Sub

        'The rest of the (not Shared) methods in
        ' this class don't have to test security--
        'they won't even exist if the above constructor test fails.
        Public Function SaveFile() As String
             ' do some I//O here
        End Function

    End Class

Use the Demand method, as illustrated in this code, to make certain that callers are allowed to access something (in this example, files). Here, before allowing this class to be instantiated, you demand a security check. The entire call stack is checked and all must have permission. If there is no security exception thrown, then the Demand is met.

You can use this security check class to test individual methods within the class (as illustrated by the SaveFile method in Listing 5.2 above), or you could have this class generate a special key that the caller can use during the entire session with your application.

TIP For simplicity I used FileIOPermission in the example in Listing 5.2; however, note that the .NET security system automatically demands File I/O permissions (and other, similar sensitive resource permissions). You don't typically need to write special code for this kind of thing. However, you can use these techniques to provide additional protection within database access procedures and other situations.

Summary

In this chapter, you saw the ways that a programmer can address security issues to prevent hackers from breaching a system via your application, from using your application to access sensitive resources, and from other kinds of attack.

You saw that application security is divided into two primary levels: role-based (derived from the user logon) and code-based (derived from assertions or denials made within .NET code itself).

You saw how Windows built-in permissions groups are accessed and what they mean. Then code-access security (CAS) was examined, and how it interacts with role-based security features. You worked with the Framework Configuration tool, and saw how to employ various of its features to specify how .NET security is enforced. Finally, you explored some of the ways that you can protect consumed code such as Web services by setting up a permissions gateway through which the caller(s) must pass.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net