Professional Documents
Culture Documents
Solution: Securitymanager - Isgranted
Solution: Securitymanager - Isgranted
Check to see if your assembly received the optional permissions using the
SecurityManager.IsGranted method like this:
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.Security;
This code sets up a Regex for the O’Reilly web site and then uses it to create a
WebPermission for connecting to that site and all sites containing the string. You then
check the WebPermission by calling SecurityManager.IsGranted to see whether you
have permission to do this.
Discussion
The IsGranted method is a lightweight way of determining whether permission is
granted for an assembly without first incurring the full stackwalk that a Demand gives
you. Note, however, that once you exercise the code that performs the Demand, the
full stackwalk will then take place. The drawback to this approach is that the code is
still subject to a luring attack if Assert is misused, so you need to consider where the
call to IsGranted is being made in the overall scheme of your security.
Some of the reasons you might design an assembly to have optional permissions is
for deployment in different customer scenarios. In some scenarios (such as desktop
applications), it might be acceptable to have an assembly that can perform more
robust actions (talk to a database, create network traffic via HTTP, etc.). In other
scenarios, you can defer these actions if the customer does not wish to grant enough
permissions for these extra services to function.
See Also
The “WebPermission Class,” “SecurityManager Class,” and “IsGranted Method”
topics in the MSDN documentation.
Index | 851
Finally, you have a method that calls the CreatePoint method. This method handles
the point object type returned from the CreatePoint method based on the actual
point object returned:
public void CreateAndHandlePoint( )
{
// Create a new point object and return it.
object retObj = CreatePoint(PointTypeEnum.Point2D);
Notice that the tests for the ExPoint2D and ExPoint3D objects are performed before
the tests for Point2D and Point3D. This order will allow you to differentiate between
base classes and their derived classes (ExPoint2D derives from Point2D and ExPoint3D
derives from Point3D). If you had reversed these tests, the test for Point2D would eval-
uate to true for both the Point2D class and its derivatives (ExPoint2D).
Discussion
The is operator is a fast and easy method of predetermining whether a cast will
work. If the cast fails, you have saved yourself the overhead of trying the cast and
handling a thrown exception. If the is operator determines that this cast can success-
fully be performed, all you need to do is perform the cast.
The is operator is defined as follows:
expression is type
How to tear this invoice apart using an XElement and send the various information
pieces to their respective departments is shown in Example 15-8.
See Also
The “FileVersionInfo Class” topic in the MSDN documentation.
Solution
Use the various properties in the DriveInfo class as shown here:
public static void DisplayAllDriveInfo( )
{
foreach (DriveInfo drive in DriveInfo.GetDrives( ))
{
if (drive.IsReady)
{
Console.WriteLine("Drive " + drive.Name + " is ready.");
Console.WriteLine("AvailableFreeSpace: " + drive.AvailableFreeSpace);
Console.WriteLine("DriveFormat: " + drive.DriveFormat);
Console.WriteLine("DriveType: " + drive.DriveType);
Console.WriteLine("Name: " + drive.Name);
Console.WriteLine("RootDirectory.FullName: " +
drive.RootDirectory.FullName);
Console.WriteLine("TotalFreeSpace: " + drive.TotalFreeSpace);
Console.WriteLine("TotalSize: " + drive.TotalSize);
Console.WriteLine("VolumeLabel: " + drive.VolumeLabel);
}
else
{
Console.WriteLine("Drive " + drive.Name + " is not ready.");
}
}
}