FirstMatchCodeGroup 類別

定義

警告

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

警告

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

警告

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

警告

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.

允許由程式碼群組和第一個相符子程式碼群組的原則陳述式聯合定義安全性原則。 此類別無法獲得繼承。

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
繼承
FirstMatchCodeGroup
屬性

範例

下列範例顯示 類別成員的使用 FirstMatchCodeGroup

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.

備註

注意

程式碼啟用安全性 (CAS) 在所有版本的 .NET Framework 和 .NET 中已被取代。 最新版本的 .NET 不接受 CAS 批註,並在使用 CAS 相關 API 時產生錯誤。 開發人員應該尋求替代方式來完成安全性工作。

程式碼群組是程式碼存取安全性原則的建置組塊。 每個原則層級都包含可具有副程式代碼群組的根程式碼群組。 每個副程式代碼群組都可以有自己的副程式代碼群組;此行為會延伸到任意數目的層級,形成樹狀結構。 每個程式碼群組都有一個成員資格條件,根據該元件的辨識項,判斷指定的元件是否屬於它。 只有成員資格條件符合指定元件及其副程式代碼群組的程式碼群組會套用原則。

就像任何程式碼群組一樣, FirstMatchCodeGroup 只有在其成員資格條件符合元件的辨識項時才適用。 如果有相符專案,它會依序測試每個子系的成員資格條件,並在發生第一次比對時停止。 的結果 FirstMatchCodeGroup 是根程式碼群組之原則語句的聯集,以及符合該程式碼群組之第一個子群組的原則語句。

FirstMatchCodeGroup 適用于應用程式域主機設定網域原則的程式設計用途。

建構函式

FirstMatchCodeGroup(IMembershipCondition, PolicyStatement)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

初始化 FirstMatchCodeGroup 類別的新執行個體。

屬性

AttributeString
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得程式碼群組之原則陳述式屬性 (Attribute) 的字串表示。

(繼承來源 CodeGroup)
Children
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得或設定程式碼群組的子程式碼群組之排序清單。

(繼承來源 CodeGroup)
Description
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得或設定程式碼群組的描述。

(繼承來源 CodeGroup)
MembershipCondition
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得或設定程式碼群組的成員資格條件。

(繼承來源 CodeGroup)
MergeLogic
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得合併邏輯。

Name
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得或設定程式碼群組的名稱。

(繼承來源 CodeGroup)
PermissionSetName
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得程式碼群組的具名使用權限之名稱。

(繼承來源 CodeGroup)
PolicyStatement
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得或設定與程式碼群組相關的原則陳述式。

(繼承來源 CodeGroup)

方法

AddChild(CodeGroup)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

加入子程式碼群組到目前程式碼群組。

(繼承來源 CodeGroup)
Copy()
已淘汰.
已淘汰.
已淘汰.
已淘汰.

製作程式碼群組的深層複本 (Deep Copy)。

CreateXml(SecurityElement, PolicyLevel)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

在衍生類別中覆寫時,序列化衍生的程式碼群組特定的屬性和內部狀態,並加入序列化到指定的 SecurityElement

(繼承來源 CodeGroup)
Equals(CodeGroup, Boolean)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

判斷指定的程式碼群組是否等於目前程式碼群組,若有指定則檢查子程式碼群組。

(繼承來源 CodeGroup)
Equals(Object)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

判斷指定的程式碼群組是否相等於目前程式碼群組。

(繼承來源 CodeGroup)
FromXml(SecurityElement)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

使用 XML 編碼方式重建具有指定狀態的安全性物件。

(繼承來源 CodeGroup)
FromXml(SecurityElement, PolicyLevel)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

使用 XML 編碼方式重建具有指定狀態和原則層級的安全性物件。

(繼承來源 CodeGroup)
GetHashCode()
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得目前程式碼群組的雜湊碼。

(繼承來源 CodeGroup)
GetType()
已淘汰.
已淘汰.
已淘汰.
已淘汰.

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()
已淘汰.
已淘汰.
已淘汰.
已淘汰.

建立目前 Object 的淺層複製。

(繼承來源 Object)
ParseXml(SecurityElement, PolicyLevel)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

在衍生類別中覆寫時,從指定的 SecurityElement 重建衍生程式碼群組特定的屬性和內部狀態。

(繼承來源 CodeGroup)
RemoveChild(CodeGroup)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

移除指定的子程式碼群組。

(繼承來源 CodeGroup)
Resolve(Evidence)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

解析程式碼群組的原則和其辨識項集合的子代。

ResolveMatchingCodeGroups(Evidence)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

解析符合的程式碼群組。

ToString()
已淘汰.
已淘汰.
已淘汰.
已淘汰.

傳回代表目前物件的字串。

(繼承來源 Object)
ToXml()
已淘汰.
已淘汰.
已淘汰.
已淘汰.

建立安全物件及其目前狀態的 XML 編碼方式。

(繼承來源 CodeGroup)
ToXml(PolicyLevel)
已淘汰.
已淘汰.
已淘汰.
已淘汰.

建立安全性物件、它的目前狀態和程式碼所存在的原則層級的 XML 編碼方式。

(繼承來源 CodeGroup)

適用於

產品 版本 (已過時)
.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)