Auf Englisch lesen

Freigeben über


PolicyStatement Klasse

Definition

Stellt die Anweisung einer CodeGroup dar, die die Berechtigungen und anderen Informationen beschreibt, die für Code mit einer bestimmten Gruppe von Nachweisen gelten. Diese Klasse kann nicht geerbt werden.

C#
public sealed class PolicyStatement : System.Security.ISecurityEncodable, System.Security.ISecurityPolicyEncodable
C#
[System.Serializable]
public sealed class PolicyStatement : System.Security.ISecurityEncodable, System.Security.ISecurityPolicyEncodable
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class PolicyStatement : System.Security.ISecurityEncodable, System.Security.ISecurityPolicyEncodable
Vererbung
PolicyStatement
Attribute
Implementiert

Beispiele

Das folgende Codebeispiel zeigt die Verwendung von Membern der PolicyStatement-Klasse.

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

class Members
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create two new policy statements.
        PolicyStatement policyStatement = firstConstructorTest();
        PolicyStatement policyStatement2 = secondConstructorTest();

        // Add attributes to the first policy statement.
        policyStatement.Attributes = PolicyStatementAttribute.All;

        // Create a copy of the first policy statement.
        PolicyStatement policyStatementCopy = createCopy(policyStatement);
        addXmlMember(ref policyStatementCopy);

        summarizePolicyStatment(policyStatement);
        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Construct a PolicyStatement with an Unrestricted permission set.
    private static PolicyStatement firstConstructorTest() 
    {
        // Construct the permission set.
        PermissionSet permissions 
            = new PermissionSet(PermissionState.Unrestricted);
        permissions.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));
        permissions.AddPermission(
            new ZoneIdentityPermission(SecurityZone.MyComputer));

        // Create a policy statement based on the newly created permission
        // set.
        PolicyStatement policyStatement = new PolicyStatement(permissions);

        return policyStatement;
    }

    // Construct a PolicyStatement with an Unrestricted permission set and
    // the LevelFinal attribute.
    private static PolicyStatement secondConstructorTest()
    {
        // Construct the permission set.
        PermissionSet permissions =
            new PermissionSet(PermissionState.Unrestricted);
        permissions.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));
        permissions.AddPermission(
            new ZoneIdentityPermission(SecurityZone.MyComputer));

        PolicyStatementAttribute levelFinalAttribute = 
            PolicyStatementAttribute.LevelFinal;
        
        // Create a new policy statement with the specified permission set.
        // The LevelFinal attribute is set to prevent the evaluation of lower
        // policy levels in a resolve operation.
        PolicyStatement policyStatement =
            new PolicyStatement(permissions, levelFinalAttribute);

        return policyStatement;
    }

    // Add a named permission set to the specified PolicyStatement.
    private static void AddPermissions(ref PolicyStatement policyStatement)
    {
        // Construct a NamedPermissionSet with basic permissions.
        NamedPermissionSet allPerms = new NamedPermissionSet("allPerms");
        allPerms.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));
        allPerms.AddPermission(
            new ZoneIdentityPermission(SecurityZone.MyComputer));
        allPerms.AddPermission(
            new SiteIdentityPermission("www.contoso.com"));

        policyStatement.PermissionSet = allPerms;
    }

    // If a class attribute is not found in the specified PolicyStatement,
    // add a child XML element with an added class attribute.
    private static void addXmlMember(ref PolicyStatement policyStatement) 
    {
        SecurityElement xmlElement = policyStatement.ToXml();
        if (xmlElement.Attribute("class") == null)
        {
            SecurityElement newElement = 
                new SecurityElement("PolicyStatement");
            newElement.AddAttribute("class", policyStatement.ToString());
            newElement.AddAttribute("version","1.1");

            newElement.AddChild(new SecurityElement("PermissionSet"));

            policyStatement.FromXml(newElement);

            Console.Write("Added the class attribute and modified its ");
            Console.WriteLine("version number.\n" + newElement.ToString());
        }
    }

    // Verify that the type of the specified object is a PolicyStatement type
    // then create a copy of the object.
    private static PolicyStatement createCopy(Object sourceObject)
    {
        PolicyStatement returnedStatement = new PolicyStatement(null);
        // Compare specified object type with the PolicyStatement type.
        if (sourceObject.GetType().Equals(typeof(PolicyStatement)))
        {
            returnedStatement = getCopy((PolicyStatement)sourceObject);
        }
        else
        {
            throw new ArgumentException("Expected the PolicyStatement type.");
        }

        return returnedStatement;
    }

    // Return a copy of the specified PolicyStatement if the result of the
    // Copy command is an equivalent object. Otherwise, return the
    // original PolicyStatement object.
    private static PolicyStatement getCopy(PolicyStatement policyStatement)
    {
        // Create an equivalent copy of the policy statement.
        PolicyStatement policyStatementCopy = policyStatement.Copy();

        // Compare the specified objects for equality.
        if (!policyStatementCopy.Equals(policyStatement))
        {
            return policyStatementCopy;
        } 
        else 
        {
            return policyStatement;
        }
    }

    // Summarize the attributes of the specified PolicyStatement on the
    // console window.
    private static void summarizePolicyStatment(
        PolicyStatement policyStatement)
    {
        // Retrieve the class path for policyStatement.
        string policyStatementClass = policyStatement.ToString();

        int hashCode = policyStatement.GetHashCode();

        string attributeString = "";
        // Retrieve the string representation of the PolicyStatement
        // attributes.
        if (policyStatement.AttributeString != null)
        {
            attributeString = policyStatement.AttributeString;
        }

        // Write a summary to the console window.
        Console.WriteLine("\n*** " + policyStatementClass + " summary ***");
        Console.Write("This PolicyStatement has been created with hash ");
        Console.Write("code(" + hashCode + ") ");

        Console.Write("and contains the following attributes: ");
        Console.WriteLine(attributeString);
    }
}
//
// This sample produces the following output:
//
// Added the class attribute and modified the version number.
// <PolicyStatement class="System.Security.Policy.PolicyStatement"
//                  version="1.1">
//    <PermissionSet/>
// 
// *** System.Security.Policy.PolicyStatement summary ***
// PolicyStatement has been created with hash code(20) containing the
// following attributes: Exclusive LevelFinal
// This sample completed successfully; press Enter to exit.

Hinweise

Ein PolicyStatement besteht aus einer Reihe von erteilten Berechtigungen und möglichen speziellen Attributen für die Codegruppe.

Richtlinienanweisungen werden in der Regel als Rückgabewert eines Resolve Vorgangs für eine PolicyLevelverwendet.

Konstruktoren

PolicyStatement(PermissionSet, PolicyStatementAttribute)
Veraltet.

Initialisiert eine neue Instanz der PolicyStatement-Klasse mit den angegebenen PermissionSet und Attributen.

PolicyStatement(PermissionSet)
Veraltet.

Initialisiert eine neue Instanz der PolicyStatement Klasse mit dem angegebenen PermissionSet.

Eigenschaften

Attributes

Ruft die Attribute der Richtlinienausweisung ab oder legt diese fest.

AttributeString

Ruft eine Zeichenfolgendarstellung der Attribute der Richtlinienausweisung ab.

PermissionSet
Veraltet.

Dient zum Abrufen oder Festlegen des PermissionSet der Richtlinienausweisung.

Methoden

Copy()

Erstellt eine entsprechende Kopie der aktuellen Richtlinienausweisung.

Equals(Object)

Bestimmt, ob das angegebene PolicyStatement -Objekt dem aktuellen PolicyStatemententspricht.

Equals(Object)

Bestimmt, ob das angegebene Objekt dem aktuellen Objekt entspricht.

(Geerbt von Object)
FromXml(SecurityElement, PolicyLevel)

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

FromXml(SecurityElement)

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

GetHashCode()

Ruft einen Hashcode für das PolicyStatement-Objekt ab, das für die Verwendung in Hashingalgorithmen und Datenstrukturen wie einer Hashtabelle geeignet ist.

GetHashCode()

Dient als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft die Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie der aktuellen Object.

(Geerbt von Object)
ToString()

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

(Geerbt von Object)
ToXml()

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

ToXml(PolicyLevel)

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

Gilt für:

Produkt Versionen
.NET 6, 7, 8, 9
.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, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9