Evidence Class


Defines the set of information that constitutes input to security policy decisions. This class cannot be inherited.

Namespace:   System.Security.Policy
Assembly:  mscorlib (in mscorlib.dll)


public ref class Evidence sealed : ICollection, IEnumerable


Initializes a new empty instance of the Evidence class.


Initializes a new instance of the Evidence class from a shallow copy of an existing one.

System_CAPS_pubmethodEvidence(array<EvidenceBase^>^, array<EvidenceBase^>^)

Initializes a new instance of the Evidence class from multiple sets of host and assembly evidence.

System_CAPS_pubmethodEvidence(array<Object^>^, array<Object^>^)

Obsolete. Initializes a new instance of the Evidence class from multiple sets of host and assembly evidence.


Obsolete. Gets the number of evidence objects in the evidence set.


Gets a value indicating whether the evidence set is read-only.


Gets a value indicating whether the evidence set is thread-safe.


Gets or sets a value indicating whether the evidence is locked.


Gets the synchronization root.


Obsolete. Adds the specified assembly evidence to the evidence set.


Adds an evidence object of the specified type to the assembly-supplied evidence list.


Obsolete. Adds the specified evidence supplied by the host to the evidence set.


Adds host evidence of the specified type to the host evidence collection.


Removes the host and assembly evidence from the evidence set.


Returns a duplicate copy of this evidence object.

System_CAPS_pubmethodCopyTo(Array^, Int32)

Obsolete. Copies evidence objects to an Array.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Enumerates evidence provided by the assembly.


Gets assembly evidence of the specified type from the collection.


Obsolete. Enumerates all evidence in the set, both that provided by the host and that provided by the assembly.


Serves as the default hash function. (Inherited from Object.)


Enumerates evidence supplied by the host.


Gets host evidence of the specified type from the collection.


Gets the Type of the current instance.(Inherited from Object.)


Merges the specified evidence set into the current evidence set.


Removes the evidence for a given type from the host and assembly enumerations.


Returns a string that represents the current object.(Inherited from Object.)


Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)


Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)


Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)


Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

Common forms of evidence include signatures and location of origin of code, but can potentially be anything. Objects of any type that are recognized by security policy represent evidence.

Security policy is composed of code groups; a particular assembly (the basic unit of code for granting security permissions) is a member of a code group if it satisfies the code group's membership condition. Evidence is the set of inputs to policy that membership conditions use to determine to which code groups an assembly belongs.

The Evidence class is a collection (see ICollection) that holds a set of objects that represent evidence. This class holds two sets that correspond to the source of the evidence: host evidence and assembly evidence.

Policy can get evidence from two different sources when evaluating permissions for code.

  • Host evidence is provided by the host, and can only be provided by hosts that have been granted the ControlEvidence permission. Typically, this is evidence of the origin of the code and digital signatures on the assembly. Evidence about origin typically includes Url, Site, and Zone evidence. Signatures refer to software publisher (AuthentiCode X.509v3 signature) and strong name identities. Both kinds of digital signature-based identity are built into the assembly, but must be validated and passed to policy by the host; when loaded, the security system verifies the signature. The system then creates the appropriate evidence and passes it to policy only if the corresponding signature is valid.

  • Assembly evidence is part of the assembly itself. Developers or administrators can attach custom evidence to the assembly to extend the set of evidence for policy. Assembly evidence can only be added at the time of assembly generation, which occurs before the assembly is signed. The default policy of the security system ignores assembly-provided evidence, but policy can be extended to accept it.

The following code example demonstrates how to create new Evidence classes with both host evidence and assembly evidence.

using namespace System;
using namespace System::Collections;
using namespace System::Security;
using namespace System::Security::Policy;
using namespace System::Security::Permissions;
using namespace System::Globalization;
public ref class Evidence_Example
   bool CreateEvidence()
      bool retVal = true;
         // Create empty evidence using the default contructor.
         Evidence^ ev1 = gcnew Evidence;
         Console::WriteLine( "Created empty evidence with the default constructor." );

         // Constructor used to create null host evidence.
         Evidence^ ev2a = gcnew Evidence( nullptr );
         Console::WriteLine( "Created an Evidence object with null host evidence." );

         // Constructor used to create host evidence.
         Url^ url = gcnew Url( "http://www.treyresearch.com" );
         Console::WriteLine( "Adding host evidence {0}", url );
         ev2a->AddHost( url );
         Evidence^ ev2b = gcnew Evidence( ev2a );
         Console::WriteLine( "Copy evidence into new evidence" );
         IEnumerator^ enum1 = ev2b->GetHostEnumerator();
         Console::WriteLine( enum1->Current );

         // Constructor used to create both host and assembly evidence.
         array<Object^>^oa1 = {};
         Site^ site = gcnew Site( "www.wideworldimporters.com" );
         array<Object^>^oa2 = {url,site};
         Evidence^ ev3a = gcnew Evidence( oa1,oa2 );
         enum1 = ev3a->GetHostEnumerator();
         IEnumerator^ enum2 = ev3a->GetAssemblyEnumerator();
         Object^ obj1 = enum2->Current;
         Console::WriteLine( "URL = {0}  Site = {1}", obj1, enum2->Current );

         // Constructor used to create null host and null assembly evidence.
         Evidence^ ev3b = gcnew Evidence( (array<Object^>^)nullptr, (array<Object^>^)nullptr );
         Console::WriteLine( "Create new evidence with null host and assembly evidence" );
      catch ( Exception^ e )
         Console::WriteLine( "Fatal error: {0}", e );
         return false;

      return retVal;

   Evidence^ DemonstrateEvidenceMembers()
      Evidence^ myEvidence = gcnew Evidence;
      String^ sPubKeyBlob = "00240000048000009400000006020000"
      array<Byte>^pubkey = HexsToArray( sPubKeyBlob );

      // Create a strong name.
      StrongName^ mSN = gcnew StrongName( gcnew StrongNamePublicKeyBlob( pubkey ),"SN01",gcnew Version( "" ) );

      // Create assembly and host evidence.
      Console::WriteLine( "Adding assembly evidence." );
      myEvidence->AddAssembly( "SN01" );
      myEvidence->AddAssembly( gcnew Version( "" ) );
      myEvidence->AddAssembly( mSN );
      Console::WriteLine( "Count of evidence items = {0}", myEvidence->Count );

      Url^ url = gcnew Url( "http://www.treyresearch.com" );
      Console::WriteLine( "Adding host evidence {0}", url );
      myEvidence->AddHost( url );
      PrintEvidence( myEvidence ).ToString();
      Console::WriteLine( "Count of evidence items = {0}", myEvidence->Count );

      Console::WriteLine( "\nCopy the evidence to an array using CopyTo, then display the array." );
      array<Object^>^evidenceArray = gcnew array<Object^>(myEvidence->Count);
      myEvidence->CopyTo( evidenceArray, 0 );
      for each (Object^ obj in evidenceArray)

      Console::WriteLine( "\nDisplay the contents of the properties." );
      Console::WriteLine( "Locked is the only property normally used by code." );
      Console::WriteLine( "IsReadOnly, IsSynchronized, and SyncRoot properties are not normally used." );

      Console::WriteLine( "\nThe default value for the Locked property = {0}", myEvidence->Locked );

      Console::WriteLine( "\nGet the hashcode for the evidence." );
      Console::WriteLine( "HashCode = {0}", myEvidence->GetHashCode() );

      Console::WriteLine( "\nGet the type for the evidence." );
      Console::WriteLine( "Type = {0}", myEvidence->GetType() );

      Console::WriteLine( "\nMerge new evidence with the current evidence." );
      array<Object^>^oa1 = {};
      Site^ site = gcnew Site( "www.wideworldimporters.com" );
      array<Object^>^oa2 = {url,site};
      Evidence^ newEvidence = gcnew Evidence( oa1,oa2 );
      myEvidence->Merge( newEvidence );
      Console::WriteLine( "Evidence count = {0}", PrintEvidence( myEvidence ) );

      Console::WriteLine( "\nRemove URL evidence." );
      myEvidence->RemoveType( url->GetType() );
      Console::WriteLine( "Evidence count is now: {0}", myEvidence->Count );

      Console::WriteLine( "\nMake a copy of the current evidence." );
      Evidence^ evidenceCopy = gcnew Evidence( myEvidence );
      Console::WriteLine( "Count of new evidence items = {0}", evidenceCopy->Count );
      Console::WriteLine( "Does the copy equal the current evidence? {0}", myEvidence->Equals( evidenceCopy ) );

      Console::WriteLine( "\nClear the current evidence." );
      Console::WriteLine( "Count is now {0}", myEvidence->Count );

      return myEvidence;

   static int PrintEvidence( Evidence^ myEvidence )
      int p = 0;
      Console::WriteLine( "\nCurrent evidence = " );
      if ( nullptr == myEvidence )
            return 0;

      IEnumerator^ list = myEvidence->GetEnumerator();
      IEnumerator^ myEnum1 = list;
      while ( myEnum1->MoveNext() )
         Object^ obj = safe_cast<Object^>(myEnum1->Current);
         Console::WriteLine( obj );

      Console::WriteLine( "\n" );
      return p;

   // Convert a hexadecimal string to an array.
   static array<Byte>^ HexsToArray( String^ sHexString )
      array<Byte>^arr = gcnew array<Byte>(sHexString->Length / 2);
      for ( int i = 0; i < sHexString->Length; i += 2 )
         arr[ i / 2 ] = Byte::Parse( sHexString->Substring( i, 2 ), NumberStyles::HexNumber );

      return arr;

// Main method.
int main()
      Evidence_Example^ EvidenceTest = gcnew Evidence_Example;
      bool ret = EvidenceTest->CreateEvidence();
      if ( ret )
         Console::WriteLine( "Evidence successfully created." );
         Console::WriteLine( "Evidence creation failed." );
   catch ( Exception^ e ) 
      Console::WriteLine( e );
      Environment::ExitCode = 101;

.NET Framework
Available since 1.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top