ZoneCodeGroup Class

Provides a code group that uses the zone from which the assembly was downloaded as the membership condition.

Assembly

System.dll

Inheritance Hierarchy

Object
CodeGroup
ZoneCodeGroup

Remarks

The ZoneCodeGroup class is used to create code groups that grant permissions based on the download zone of an assembly. The .NET Framework uses the Zone enumeration to represent different security zones. When an assembly is loaded, the Code Access Security (CAS) policy provider can evaluate its download zone and determine the appropriate code group membership.

Note: Code Access Security (CAS) is deprecated in .NET Core and later versions. This class is primarily relevant for applications targeting older versions of the .NET Framework.

Constructors


public ZoneCodeGroup()

Initializes a new instance of the ZoneCodeGroup class with default settings.


public ZoneCodeGroup(
    SecurityElement el
)

Initializes a new instance of the ZoneCodeGroup class from the specified XML serialization store.

Parameters

el
A System.Security.SecurityElement object that represents a code group and its properties.

Methods


public override bool Equals(
    object o
)

Determines whether the specified object is equal to the current object.


public override int GetHashCode()

Serves as the default hash function.


public override PolicyStatement GetPolicyStatement(
    IMembershipCondition membershipCondition
)

Creates and returns a new policy statement for the specified membership condition.

Parameters

membershipCondition
The IMembershipCondition for which to create a policy statement.

Returns

A PolicyStatement object that represents the permissions for the specified membership condition.

public override System.Collections.IEnumerator GetRules()

Returns an enumerator for the child code groups.


public override IMembershipCondition GetMembershipCondition()

Gets the membership condition for the code group.

Returns

The IMembershipCondition for the code group.

public override void FromXml(
    SecurityElement e,
    IPolicyFactory policyFactory
)

Reconstructs a code group object from the specified XML element.

Parameters

e
The XML element used to reconstruct the code group.
policyFactory
The policy factory to use for creating code groups.

public override SecurityElement ToXml()

Returns an XML encoding of the code group, which can be used to serialize a castom policy level.

Returns

A System.Security.SecurityElement object that represents the code group.

Properties


public string Description { get; set; }

Gets or sets a description of the code group.


public int MergeLimit { get; set; }

Gets or sets the merge limit for the code group.


public string Name { get; set; }

Gets or sets the name of the code group.

Example

The following code example demonstrates how to create a ZoneCodeGroup and add it to a policy level.


using System;
using System.Security;
using System.Security.Policy;
using System.Collections;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a new ZoneCodeGroup for the Internet zone
        ZoneCodeGroup internetZoneGroup = new ZoneCodeGroup();
        internetZoneGroup.Name = "InternetZoneGroup";
        internetZoneGroup.Description = "Code group for assemblies downloaded from the Internet.";

        // Create a membership condition for the Internet zone
        ZoneMembershipCondition internetZoneCondition = new ZoneMembershipCondition(SecurityZone.Internet);
        internetZoneGroup.MembershipCondition = internetZoneCondition;

        // Create a policy statement granting full trust
        PolicyStatement fullTrustStatement = new PolicyStatement(
            new PermissionSet(null), // null means grant all permissions
            PolicyStatementAttribute.Nothing
        );
        internetZoneGroup.PolicyStatement = fullTrustStatement;

        // You would typically add this to an IPolicyLevel object
        // For demonstration purposes, we'll just show its creation.
        Console.WriteLine($"Created ZoneCodeGroup: {internetZoneGroup.Name}");
        Console.WriteLine($"Membership Condition: {internetZoneGroup.MembershipCondition.GetType().Name}");
        Console.WriteLine($"Policy Statement Permissions: {internetZoneGroup.PolicyStatement.PermissionSet.IsFullyTrusted}");
    }
}

Requirements

Platform Requirements: Works on .NET Framework 1.1 and later.

Namespace: System.Net.Security

Assembly: System.dll