Auf Englisch lesen

Freigeben über


FirstMatchCodeGroup Klasse

Definition

Achtung

This type is obsolete. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

Achtung

This type is obsolete. See https://go.microsoft.com/fwlink/?LinkID=155570 for more information.

Achtung

Code Access Security is not supported or honored by the runtime.

Achtung

This type is obsolete and will be removed in a future release of the .NET Framework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

Ermöglicht die Definition der Sicherheitsrichtlinien durch die Gesamtmenge der Richtlinienanweisung einer Codegruppe und derjenigen der ersten untergeordneten Codegruppe, die übereinstimmt. Diese Klasse kann nicht vererbt werden.

C#
[System.Obsolete("This type is obsolete. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public sealed class FirstMatchCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Obsolete("This type is obsolete. See https://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public sealed class FirstMatchCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Obsolete("Code Access Security is not supported or honored by the runtime.")]
public sealed class FirstMatchCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Serializable]
public sealed class FirstMatchCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class FirstMatchCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Obsolete("This type is obsolete and will be removed in a future release of the .NET Framework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public sealed class FirstMatchCodeGroup : System.Security.Policy.CodeGroup
Vererbung
FirstMatchCodeGroup
Attribute

Beispiele

Das folgende Beispiel zeigt die Verwendung von Membern der FirstMatchCodeGroup -Klasse.

C#
using System;
using System.Security;
using System.Security.Policy;
using System.Security.Permissions;
using System.Reflection;

class Members
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create a new FirstMatchCodeGroup.
        FirstMatchCodeGroup codeGroup = constructDefaultGroup();

        // Create a deep copy of the FirstMatchCodeGroup.
        FirstMatchCodeGroup copyCodeGroup = 
            (FirstMatchCodeGroup)codeGroup.Copy();
        // Compare the original code group with the copy.
        CompareTwoCodeGroups(codeGroup, copyCodeGroup);

        addPolicy(ref codeGroup);
        addXmlMember(ref codeGroup);
        updateMembershipCondition(ref codeGroup);
        addChildCodeGroup(ref codeGroup);

        Console.Write("Comparing the resolved code group ");
        Console.WriteLine("with the initial code group.");
        FirstMatchCodeGroup resolvedCodeGroup =
            ResolveGroupToEvidence(codeGroup);
        if (CompareTwoCodeGroups(codeGroup, resolvedCodeGroup))
        {
            PrintCodeGroup(resolvedCodeGroup);
        }
        else
        {
            PrintCodeGroup(codeGroup);
        }
        
        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Create a FirstMatchCodeGroup with an exclusive policy and membership
    // condition.
    private static FirstMatchCodeGroup constructDefaultGroup()
    {
        // Construct a new FirstMatchCodeGroup with Read, Write, Append
        // and PathDiscovery access.
        // Create read access permission to the root directory on drive C.
        FileIOPermission rootFilePermissions =
            new FileIOPermission(PermissionState.None);
        rootFilePermissions.AllLocalFiles = FileIOPermissionAccess.Read;
        rootFilePermissions.SetPathList(FileIOPermissionAccess.Read,"C:\\");

        // Add a permission to a named permission set.
        NamedPermissionSet namedPermissions =
            new NamedPermissionSet("RootPermissions");
        namedPermissions.AddPermission(rootFilePermissions);

        // Create a PolicyStatement with exclusive rights to the policy.
        PolicyStatement policy = new PolicyStatement(
            namedPermissions,PolicyStatementAttribute.Exclusive);

        // Create a FirstMatchCodeGroup with a membership condition that
        // matches all code, and an exclusive policy.
        FirstMatchCodeGroup codeGroup =
            new FirstMatchCodeGroup(
            new AllMembershipCondition(),
            policy);

        // Set the name of the first match code group.
        codeGroup.Name = "TempCodeGroup";

        // Set the description of the first match code group.
        codeGroup.Description = "Temp folder permissions group";

        return codeGroup;
    }

    // Add file permission to restrict write access to all files 
    // on the local machine.
    private static void addPolicy(ref FirstMatchCodeGroup codeGroup)
    {
        // Set the PolicyStatement property to a policy with read access to
        // the root directory on drive C.
        FileIOPermission rootFilePermissions =
            new FileIOPermission(PermissionState.None);
        rootFilePermissions.AllLocalFiles = FileIOPermissionAccess.Read;
        rootFilePermissions.SetPathList(FileIOPermissionAccess.Read,"C:\\");

        NamedPermissionSet namedPermissions =
            new NamedPermissionSet("RootPermissions");
        namedPermissions.AddPermission(rootFilePermissions);

        // Create a PolicyStatement with exclusive rights to the policy.
        PolicyStatement policy = new PolicyStatement(
            namedPermissions,
            PolicyStatementAttribute.Exclusive);

        codeGroup.PolicyStatement = policy;
    }

    // Set the membership condition of the code group.
    private static void updateMembershipCondition(
        ref FirstMatchCodeGroup codeGroup)
    {
        // Set the membership condition of the specified FirstMatchCodeGroup
        // to the Intranet zone.
        ZoneMembershipCondition zoneCondition =
            new ZoneMembershipCondition(SecurityZone.Intranet);
        codeGroup.MembershipCondition = zoneCondition;
    }

    // Create a child code group with read-access file permissions and add it
    // to the specified code group.
    private static void addChildCodeGroup(ref FirstMatchCodeGroup codeGroup)
    {
        // Create a first match code group with read access.
        FileIOPermission rootFilePermissions = 
            new FileIOPermission(PermissionState.None);
        rootFilePermissions.AllLocalFiles = FileIOPermissionAccess.Read;
        rootFilePermissions.SetPathList(FileIOPermissionAccess.Read,"C:\\");

        PermissionSet permissions =
            new PermissionSet(PermissionState.Unrestricted);
        permissions.AddPermission(rootFilePermissions);

        FirstMatchCodeGroup tempFolderCodeGroup = new FirstMatchCodeGroup(
            new AllMembershipCondition(), 
            new PolicyStatement(permissions));

        // Set the name of the child code group and add it to 
        // the specified code group.
        tempFolderCodeGroup.Name = "Read-only code group";
        codeGroup.AddChild(tempFolderCodeGroup);
    }

    // Compare the two FirstMatchCodeGroups.
    private static bool CompareTwoCodeGroups(
        FirstMatchCodeGroup firstCodeGroup, 
        FirstMatchCodeGroup secondCodeGroup)
    {
        // Compare the two specified FirstMatchCodeGroups for equality.
        if (firstCodeGroup.Equals(secondCodeGroup))
        {
            Console.WriteLine("The two code groups are equal.");
            return true;
        }
        else 
        {
            Console.WriteLine("The two code groups are not equal.");
            return false;
        }
    }

    // Retrieve the resolved policy based on executing evidence found 
    // in the specified code group.
    private static string ResolveEvidence(CodeGroup codeGroup)
    {
        string policyString = "None";

        // Resolve the policy based on the executing assembly's evidence.
        Assembly assembly = typeof(Members).Assembly;
        Evidence executingEvidence = assembly.Evidence;

        PolicyStatement policy = codeGroup.Resolve(executingEvidence);

        if (policy != null)
        {
            policyString = policy.ToString();
        }

        return policyString;
    }

    // Retrieve the resolved code group based on the evidence from the 
    // specified code group.
    private static FirstMatchCodeGroup ResolveGroupToEvidence(
        FirstMatchCodeGroup codeGroup)
    {
        // Resolve matching code groups to the executing assembly.
        Assembly assembly = typeof(Members).Assembly;
        Evidence evidence = assembly.Evidence;
        CodeGroup resolvedCodeGroup = 
            codeGroup.ResolveMatchingCodeGroups(evidence);

        return (FirstMatchCodeGroup)resolvedCodeGroup;
    }

    // If a domain attribute is not found in the specified 
    // FirstMatchCodeGroup, add a child XML element identifying a custom
    // membership condition.
    private static void addXmlMember(ref FirstMatchCodeGroup codeGroup)
    {
        SecurityElement xmlElement = codeGroup.ToXml();

        SecurityElement rootElement = new SecurityElement("CodeGroup");

        if (xmlElement.Attribute("domain") == null) 
        {
            SecurityElement newElement = 
                new SecurityElement("CustomMembershipCondition");
            newElement.AddAttribute("class","CustomMembershipCondition");
            newElement.AddAttribute("version","1");
            newElement.AddAttribute("domain","contoso.com");

            rootElement.AddChild(newElement);

            codeGroup.FromXml(rootElement);
        }

        Console.WriteLine("Added a custom membership condition:");
        Console.WriteLine(rootElement.ToString());
    }

    // Print the properties of the specified code group to the console.
    private static void PrintCodeGroup(CodeGroup codeGroup)
    {
        // Compare the type of the specified object with the
        // FirstMatchCodeGroup type.
        if (!codeGroup.GetType().Equals(typeof(FirstMatchCodeGroup)))
        {
            throw new ArgumentException(
                "Expected the FirstMatchCodeGroup type.");
        }
        
        string codeGroupName = codeGroup.Name;
        string membershipCondition = codeGroup.MembershipCondition.ToString();
        string permissionSetName = codeGroup.PermissionSetName;

        int hashCode = codeGroup.GetHashCode();

        string mergeLogic = "";
        if (codeGroup.MergeLogic.Equals("First Match"))
        {
            mergeLogic = "with first-match merge logic";
        }

        // Retrieve the class path for the FirstMatchCodeGroup.
        string firstMatchGroupClass = codeGroup.ToString();

        string attributeString = "";
        // Retrieve the string representation of the FirstMatchCodeGroup's
        // attributes.
        if (codeGroup.AttributeString != null)
        {
            attributeString = codeGroup.AttributeString;
        }

        // Write a summary to the console window.
        Console.WriteLine("\n*** " + firstMatchGroupClass + " summary ***");
        Console.Write("A FirstMatchCodeGroup named ");
        Console.Write(codeGroupName + mergeLogic);
        Console.Write(" has been created with hash code(" + hashCode + ").");
        Console.Write("\nThis code group contains a " + membershipCondition);
        Console.Write(" membership condition with the ");
        Console.WriteLine(permissionSetName + " permission set.");

        Console.Write("The code group contains the following policy: ");
        Console.Write(ResolveEvidence(codeGroup));
        Console.Write("\nIt also contains the following attributes: ");
        Console.WriteLine(attributeString);

        int childCount = codeGroup.Children.Count;
        if (childCount > 0 )
        {
            Console.Write("There are " + childCount);
            Console.WriteLine(" child elements in the code group.");

            // Iterate through the child code groups to display their names
            // and then remove them from the specified code group.
            for (int i=0; i < childCount; i++)
            {
                // Retrieve a child code group, which has been cast as a
                // FirstMatchCodeGroup type.
                FirstMatchCodeGroup childCodeGroup = 
                    (FirstMatchCodeGroup)codeGroup.Children[i];

                Console.Write("Removing the " + childCodeGroup.Name + ".");
                // Remove the child code group.
                codeGroup.RemoveChild(childCodeGroup);
            }

            Console.WriteLine();
        }
        else
        {
            Console.WriteLine(" No child code groups were found in this" + 
                " code group.");
        }
    }
}
//
// This sample produces the following output:
//
// The two code groups are equal.
// Added a custom membership condition:
//   <CustomMembershipCondition class="CustomMembershipCondition"
//                              version="1"
//                              domain="contoso.com"/>
//
// Comparing the resolved code group with the initial code group.
// The two code groups are not equal.
//
// *** System.Security.Policy.FirstMatchCodeGroup summary ***
// A FirstMatchCodeGroup named with first-match merge logic has been created
// with hash code(113151525).
// This code group contains a Zone - Intranet membership condition with the 
// permission set. The code group contains the following policy: 
// It also contains the following attributes:
// There are 1 child elements in the code group.
// Removing the Read-only code group.
// This sample completed successfully; press Enter to exit.

Hinweise

Achtung

Code Access Security (CAS) ist in allen Versionen von .NET Framework und .NET veraltet. Aktuelle Versionen von .NET berücksichtigen CAS-Anmerkungen nicht und verursachen Fehler, wenn CAS-bezogene APIs verwendet werden. Entwickler sollten nach alternativen Mitteln suchen, um Sicherheitsaufgaben zu erledigen.

Codegruppen sind die Bausteine der Codezugriffssicherheitsrichtlinie. Jede Richtlinienebene besteht aus einer Stammcodegruppe, die über untergeordnete Codegruppen verfügen kann. Jede untergeordnete Codegruppe kann über eigene untergeordnete Codegruppen verfügen. Dieses Verhalten erstreckt sich auf eine beliebige Anzahl von Ebenen und bildet eine Struktur. Jede Codegruppe verfügt über eine Mitgliedschaftsbedingung, die anhand der Beweise für diese Assembly bestimmt, ob eine bestimmte Assembly dazu gehört. Nur Codegruppen, deren Mitgliedschaftsbedingungen mit einer bestimmten Assembly und deren untergeordneten Codegruppen übereinstimmen, wenden eine Richtlinie an.

Wie jede Codegruppe gilt nur, FirstMatchCodeGroup wenn deren Mitgliedschaftsbedingung mit Beweisen für eine Assembly übereinstimmt. Wenn eine Übereinstimmung vorhanden ist, wird die Mitgliedschaftsbedingung jedes untergeordneten Elements in der Reihenfolge überprüft und beendet, wenn die erste Übereinstimmung auftritt. Das Ergebnis von FirstMatchCodeGroup ist die Union der Richtlinienanweisung der Stammcodegruppe und der Richtlinienanweisung der ersten untergeordneten Gruppe dieser Codegruppe, die übereinstimmt.

FirstMatchCodeGroup ist für die programmgesteuerte Verwendung durch Anwendungsdomänenhosts zum Festlegen der Domänenrichtlinie vorgesehen.

Konstruktoren

FirstMatchCodeGroup(IMembershipCondition, PolicyStatement)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Initialisiert eine neue Instanz der FirstMatchCodeGroup-Klasse.

Eigenschaften

AttributeString
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft eine Zeichenfolgendarstellung der Attribute der Richtlinienanweisung für die Codegruppe ab.

(Geerbt von CodeGroup)
Children
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft eine sortierte Liste der untergeordneten Codegruppen einer Codegruppe ab oder legt diese fest.

(Geerbt von CodeGroup)
Description
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft die Beschreibung der Codegruppe ab oder legt diese fest.

(Geerbt von CodeGroup)
MembershipCondition
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft die Mitgliedschaftsbedingung der Codegruppe ab oder legt diese fest.

(Geerbt von CodeGroup)
MergeLogic
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft die Zusammenführungslogik ab.

Name
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft den Namen der Codegruppe ab oder legt diesen fest.

(Geerbt von CodeGroup)
PermissionSetName
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft den Namen des benannten Berechtigungssatzes für die Codegruppe ab.

(Geerbt von CodeGroup)
PolicyStatement
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft die der Codegruppe zugeordnete Richtlinienanweisung ab oder legt diese fest.

(Geerbt von CodeGroup)

Methoden

AddChild(CodeGroup)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Fügt der aktuellen Codegruppe eine untergeordnete Codegruppe hinzu.

(Geerbt von CodeGroup)
Copy()
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Erstellt eine tiefe Kopie der Codegruppe.

CreateXml(SecurityElement, PolicyLevel)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Beim Überschreiben in einer abgeleiteten Klasse werden die für eine abgeleitete Codegruppe spezifischen Eigenschaften sowie der interne Zustand serialisiert. Die Serialisierung wird anschließend dem angegebenen SecurityElement hinzugefügt.

(Geerbt von CodeGroup)
Equals(CodeGroup, Boolean)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Bestimmt, ob die angegebene Codegruppe mit der aktuellen Codegruppe übereinstimmt und überprüft, sofern dies angegeben ist, auch die untergeordneten Codegruppen.

(Geerbt von CodeGroup)
Equals(Object)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Bestimmt, ob die angegebene Codegruppe mit der aktuellen Codegruppe übereinstimmt.

(Geerbt von CodeGroup)
FromXml(SecurityElement)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Rekonstruiert aus einer XML-Codierung ein Sicherheitsobjekt mit einem angegebenen Zustand.

(Geerbt von CodeGroup)
FromXml(SecurityElement, PolicyLevel)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Rekonstruiert aus einer XML-Codierung ein Sicherheitsobjekt mit einem angegebenen Zustand und eine Richtlinienebene.

(Geerbt von CodeGroup)
GetHashCode()
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft den Hashcode der aktuellen Codegruppe ab.

(Geerbt von CodeGroup)
GetType()
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ParseXml(SecurityElement, PolicyLevel)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Rekonstruiert beim Überschreiben in einer abgeleiteten Klasse die spezifischen Eigenschaften sowie den internen Zustand einer aus dem angegebenen SecurityElement abgeleiteten Codegruppe.

(Geerbt von CodeGroup)
RemoveChild(CodeGroup)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Entfernt die angegebene untergeordnete Codegruppe.

(Geerbt von CodeGroup)
Resolve(Evidence)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Löst für einen Beweissatz Richtlinien für die Codegruppe und deren untergeordnete Elemente auf.

ResolveMatchingCodeGroups(Evidence)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Löst übereinstimmende Codegruppen auf.

ToString()
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)
ToXml()
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Erstellt eine XML-Codierung des Sicherheitsobjekts und seines aktuellen Zustands.

(Geerbt von CodeGroup)
ToXml(PolicyLevel)
Veraltet.
Veraltet.
Veraltet.
Veraltet.

Erstellt eine XML-Codierung des Sicherheitsobjekts und seines aktuellen Zustands sowie die Richtlinienebene, auf der der Code sich befindet.

(Geerbt von CodeGroup)

Gilt für:

Produkt Versionen (Veraltet)
.NET Framework 1.1, 2.0, 3.0, 3.5 (4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8)
Windows Desktop (3.0, 3.1, 5, 6, 7)