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.
public abstract class MembershipCondition : System.Security.ISecurityEncodable, System.Security.Policy.IMembershipCondition
Namespace: System.Security.Policy
Assembly: mscorlib.dll
System.ObjectSystem.Security.ISecurityEncodableSystem.Security.Policy.IMembershipConditionDetermines 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.
Creates and returns an identical copy of the current membership condition.
public abstract MembershipCondition Copy();
Return Value:A copy of the current membership condition.
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.
Returns the hash code for the current membership condition.
public override int GetHashCode();
Return Value:A hash code for the current membership condition.
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. |
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.
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:
Check: To test if a given evidence matches the condition.Copy: To create a deep copy of the condition.FromXml: To reconstruct the condition from its XML representation.ToXml: To convert the condition to its XML representation.Common derived classes provide specific ways to match evidence, such as by hash value, publisher, strong name, or URL.
The specific exceptions thrown by derived classes will vary, but common exceptions related to security operations might include:
System.Security.SecurityExceptionSystem.ArgumentExceptionSystem.ArgumentNullExceptionThis 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());
}
}