13.10 Manipulate Evidence as You Load an Assembly


Problem

You need to manipulate evidence as you load an assembly to affect the permissions granted to the assembly by the runtime.

Solution

Create the evidence objects that you want to assign to the assembly, add them to an instance of the class System.Security.Policy.Evidence , and then pass the Evidence collection as an argument to the method you use to load the assembly.

Discussion

The evidence possessed by an assembly defines the assembly's identity and determines which permissions the runtime grants to the assembly. The assembly loader is primarily responsible for determining what evidence to assign to an assembly, but a trusted host (such as the ASP.NET or Internet Explorer runtime hosts ) can also assign evidence to an assembly. Your code can assign evidence as you load an assembly if your code has the ControlEvidence element of SecurityPermission .

Warning  

If you try to load an assembly twice into a single application domain but assign different evidence to the assembly each time, the runtime will throw the exception System.IO.FileLoadException .

Many methods make provision for assigning evidence as you load an assembly; some are methods that load assemblies directly, whereas others instantiate types ”indirectly causing the assembly containing the requested type to load. One trait all of these methods have in common is that they each take an Evidence collection as an argument ”the Evidence class is a container for evidence objects. You must place the individual evidence objects you want to assign to the assembly in an Evidence collection and pass it to the method that loads the assembly. If you assign new evidence that conflicts with that assigned by the runtime's assembly loader, the new evidence replaces the old. Table 13-2 lists the classes and their methods that directly or indirectly load an assembly. Each method provides one or more overloads that accept an Evidence collection.

Table 13.2: Classes and Their Methods That Allow You to Assign Evidence to an Assembly

Class/Method

Description

System.Activator class

These methods affect the current application domain.

CreateInstanceCreateInstanceFrom

Instantiate a type in the current application domain from a specified assembly.

System.AppDomain class

These methods affect the application domain represented by the AppDomain object on which the method is invoked.

CreateInstance
CreateInstanceAndUnwrap
CreateInstanceFrom
CreateInstanceFromAndUnwrap

Instantiate a type from a specified assembly.

DefineDynamicAssembly

Creates a System.Reflection.Emit.AssemblyBuilder object that you can use to create an assembly dynamically in memory.

ExecuteAssembly

Loads and executes an assembly that has a defined entry point ( Main method).

Load

Loads the specified assembly.

System.Reflection.Assembly class

These methods affect the current application domain.

Load
LoadFile
LoadFrom
LoadWithPartialName

Load the specified assembly.

The following code fragment demonstrates the use of the Assembly.Load method to load an assembly into the current application domain. Before calling Load , the code creates an Evidence collection and uses its AddHost method to add Site and Zone evidence objects ( members of the System.Security.Policy namespace).

 // Create new Site and Zone evidence objects. System.Security.Policy.Site siteEvidence =      new System.Security.Policy.Site("www.microsoft.com"); System.Security.Policy.Zone zoneEvidence =      new System.Security.Policy.Zone(System.Security.SecurityZone.Trusted); // Create a new Evidence collection. System.Security.Policy.Evidence evidence =      new System.Security.Policy.Evidence(); // Add the Site and Zone evidence objects to the Evidence collection  // using the AddHost method. evidence.AddHost(siteEvidence); evidence.AddHost(zoneEvidence); // Load the assembly named "SomeAssembly" and assign the new Site and // Zone evidence objects to it. These override the Site and Zone evidence  // objects assigned by the assembly loader. System.Reflection.Assembly assembly =      System.Reflection.Assembly.Load("SomeAssembly", evidence); 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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