System.Security.Policy.MembershipCondition

Namespace: System.Security.Policy

Class Overview

Represents the condition required for an assembly to be granted a code group. Membership conditions are the fundamental building blocks of code security policies.

This is an abstract class, so you cannot create an instance of it directly. Instead, you must create an instance of a class that derives from MembershipCondition, such as AllMembershipCondition, ApplicationDirectoryMembershipCondition, HashMembershipCondition, PublisherMembershipCondition, SiteMembershipCondition, StrongNameMembershipCondition, or UrlMembershipCondition.

Syntax

public abstract class MembershipCondition : System.Security.ISecurityEncodable, System.Security.Policy.IMembershipCondition

Requirements

Namespace: System.Security.Policy

Assembly: mscorlib.dll

Members

Check(Evidence)

Determines whether the specified evidence matches the membership condition.

public abstract bool Check(Evidence evidence);

Parameters:

Name Type Description
evidence Evidence The evidence to test.

Return Value: true if the evidence matches the membership condition; otherwise, false.

Copy()

Creates and returns an identical copy of the current membership condition.

public abstract MembershipCondition Copy();

Return Value:A copy of the current membership condition.

Equals(object)

Determines whether the specified object is an instance of the same membership condition and has the same value or state as the current membership condition.

public override bool Equals(object obj);

Parameters:

Name Type Description
obj object The object to compare with the current membership condition.

Return Value: true if the specified object is equal to the current membership condition; otherwise, false.

GetHashCode()

Returns the hash code for the current membership condition.

public override int GetHashCode();

Return Value:A hash code for the current membership condition.

FromXml(SecurityElement)

Reconstructs a security object with the same state from a SecurityElement object.

public abstract void FromXml(System.Security.SecurityElement e);

Parameters:

Name Type Description
e SecurityElement The SecurityElement to use to reconstruct the security object.

ToXml()

Returns an XML encoding of the current security object and its current state in the SecurityElement class.

public abstract System.Security.SecurityElement ToXml();

Return Value:An XML encoding of the current security object.

Remarks

Membership conditions are used to define the criteria for granting code from a specific source or with specific attributes certain permissions. When an assembly is loaded, its evidence is checked against the membership conditions of code groups. If a match is found, the permissions associated with that code group are granted to the assembly.

The MembershipCondition class is the base class for all membership conditions. It defines the abstract methods that must be implemented by derived classes. These methods include:

Common derived classes provide specific ways to match evidence, such as by hash value, publisher, strong name, or URL.

Exceptions

The specific exceptions thrown by derived classes will vary, but common exceptions related to security operations might include:

Example

Illustrating the use of derived membership conditions (Conceptual)

This example shows how you might conceptually use a derived membership condition like UrlMembershipCondition within a code group. Note that direct instantiation and manipulation of code security policies are complex and often managed by the .NET runtime or administration tools.


using System;
using System.Security.Policy;
using System.Security.Principal;

public class Example
{
    public static void Main(string[] args)
    {
        // This is a simplified conceptual example.
        // Actual policy management is more involved.

        // Create evidence for an assembly from a specific URL
        Evidence assemblyEvidence = new Evidence();
        assemblyEvidence.AddHost(new Url("http://www.example.com/myapp/"));

        // Create a membership condition for the URL
        UrlMembershipCondition urlCondition = new UrlMembershipCondition("http://www.example.com/myapp/*");

        // Check if the assembly's evidence matches the condition
        if (urlCondition.Check(assemblyEvidence))
        {
            Console.WriteLine("The assembly's URL matches the membership condition.");
            // In a real scenario, this would lead to granting permissions
            // based on the associated code group.
        }
        else
        {
            Console.WriteLine("The assembly's URL does not match the membership condition.");
        }

        // Example of creating a copy
        MembershipCondition copiedCondition = urlCondition.Copy();
        Console.WriteLine($"Copied condition is of type: {copiedCondition.GetType().Name}");

        // Example of converting to XML (conceptual)
        // SecurityElement conditionXml = urlCondition.ToXml();
        // Console.WriteLine("XML representation (conceptual):");
        // Console.WriteLine(conditionXml.ToString());
    }
}