FileCodeGroup 类

定义

注意

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

向与成员资格条件匹配的代码程序集授予操作代码程序集中的操作文件的权限。 无法继承此类。

C#
[System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public sealed class FileCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Serializable]
public sealed class FileCodeGroup : System.Security.Policy.CodeGroup
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class FileCodeGroup : System.Security.Policy.CodeGroup
C#
public sealed class FileCodeGroup : System.Security.Policy.CodeGroup
继承
FileCodeGroup
属性

示例

以下示例演示 FileCodeGroup 类的成员的使用。

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)
    {
        FileCodeGroup fileCodeGroup = constructDefaultGroup();
        
        // Create a deep copy of the FileCodeGroup.
        FileCodeGroup copyCodeGroup = (FileCodeGroup)fileCodeGroup.Copy();

        CompareTwoCodeGroups(fileCodeGroup, copyCodeGroup);

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

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

    // Construct a new FileCodeGroup with Read, Write, Append 
    // and PathDiscovery access.
    private static FileCodeGroup constructDefaultGroup()
    {
        // Construct a new file code group that has complete access to
        // files in the specified path.
        FileCodeGroup fileCodeGroup = 
            new FileCodeGroup(
            new AllMembershipCondition(),
            FileIOPermissionAccess.AllAccess);

        // Set the name of the file code group.
        fileCodeGroup.Name = "TempCodeGroup";

        // Set the description of the file code group.
        fileCodeGroup.Description = "Temp folder permissions group";

        // Retrieve the string representation of the  fileCodeGroup’s 
        // attributes. FileCodeGroup does not use AttributeString, so the
        // value should be null.
        if (fileCodeGroup.AttributeString != null)
        {
            throw new NullReferenceException(
                "The AttributeString property should be null.");
        }

        return fileCodeGroup;
    }

    // Add file permission to restrict write access to all files on the
    // local machine.
    private static void addPolicy(ref FileCodeGroup fileCodeGroup)
    {
        // Set the PolicyStatement property to a policy with read access to
        // the root directory of 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);
        
        fileCodeGroup.PolicyStatement =
            new PolicyStatement(namedPermissions);
    }

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

    // Add a child group with read-access file permission to the specified 
    // code group.
    private static void addChildCodeGroup(ref FileCodeGroup fileCodeGroup)
    {
        // Create a file code group with read-access permission.
        FileCodeGroup tempFolderCodeGroup = new FileCodeGroup(
            new AllMembershipCondition(), 
            FileIOPermissionAccess.Read);

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

    // Compare the two specified file code groups for equality.
    private static bool CompareTwoCodeGroups(
        FileCodeGroup firstCodeGroup, FileCodeGroup secondCodeGroup)
    {
        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 Evidence from the executing 
    // assembly found in the specified code group.
    private static string ResolveEvidence(CodeGroup fileCodeGroup)
    {
        string policyString = "";

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

        PolicyStatement policy = fileCodeGroup.Resolve(executingEvidence);

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

        return policyString;
    }

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

        return (FileCodeGroup)codeGroup;
    }

    // If a domain attribute is not found in the specified FileCodeGroup,
    // add a child XML element identifying a custom membership condition.
    private static void addXmlMember(ref FileCodeGroup fileCodeGroup)
    {
        SecurityElement xmlElement = fileCodeGroup.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);

            fileCodeGroup.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 FileCodeGroup
        // type.
        if (!codeGroup.GetType().Equals(typeof(FileCodeGroup)))
        {
            throw new ArgumentException("Expected the FileCodeGroup type.");
        }
        
        string codeGroupName = codeGroup.Name;
        string membershipCondition = codeGroup.MembershipCondition.ToString();
        string permissionSetName = codeGroup.PermissionSetName;

        int hashCode = codeGroup.GetHashCode();

        string mergeLogic = "";
        if (codeGroup.MergeLogic.Equals("Union"))
        {
            mergeLogic = " with Union merge logic";
        }

        // Retrieve the class path for FileCodeGroup.
        string fileGroupClass = codeGroup.ToString();

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

        Console.Write("The code group has the following security policy: ");
        Console.WriteLine(ResolveEvidence(codeGroup));

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

            // Iterate through the child code groups to display their names
            // and remove them from the specified code group.
            for (int i=0; i < childCount; i++)
            {
                // Get child code group as type FileCodeGroup.
                FileCodeGroup childCodeGroup = 
                    (FileCodeGroup)codeGroup.Children[i];
                
                Console.Write("Removing the " + childCodeGroup.Name + ".");
                // Remove child code group.
                codeGroup.RemoveChild(childCodeGroup);
            }

            Console.WriteLine();
        }
        else
        {
            Console.Write("There are no child code groups");
            Console.WriteLine(" 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.FileCodeGroup summary ***
// A FileCodeGroup named  with Union merge logic has been created with hash
// code 113151473. This code group contains a Zone - Intranet membership
// condition with the Same directory FileIO - NoAccess permission set. The
// code group has the following security policy:
// There are 1 child code groups in this code group.
// Removing the Read-only group.
// This sample completed successfully; press Enter to exit.

注解

注意

代码访问安全性(CAS)已在 .NET Framework 和 .NET 的所有版本中弃用。 使用与 CAS 相关的 API 时,最新版本的 .NET 不遵循 CAS 注释并生成错误。 开发人员应寻求完成安全任务的替代方法。

代码组是代码访问安全策略的构建基块。 每个策略级别都包含一个可具有子代码组的根代码组。 每个子代码组可以有自己的子代码组;此行为扩展到任意数量的级别,形成树。 每个代码组都有一个成员资格条件,该条件根据该程序集的证据确定给定程序集是否属于该程序集。 只有成员身份条件与给定程序集及其子代码组匹配的代码组应用策略。

FileCodeGroup 具有与 UnionCodeGroup相同的子匹配语义。 但是,FileCodeGroup 返回一个权限集,其中包含一个动态计算的 FileIOPermission,用于授予对运行代码的目录的文件访问权限;UnionCodeGroup 仅返回静态权限集。 授予的文件访问权限的类型作为参数传递给构造函数。

此代码组仅匹配通过文件协议运行的程序集,即具有指向文件或 UNC 路径的 URL 的程序集。

构造函数

属性

AttributeString
已过时.

获取代码组的策略语句属性的字符串表示形式。

Children
已过时.

获取或设置代码组的子代码组的有序列表。

(继承自 CodeGroup)
Description
已过时.

获取或设置代码组的说明。

(继承自 CodeGroup)
MembershipCondition
已过时.

获取或设置代码组的成员身份条件。

(继承自 CodeGroup)
MergeLogic
已过时.

获取合并逻辑。

Name
已过时.

获取或设置代码组的名称。

(继承自 CodeGroup)
PermissionSetName
已过时.

获取代码组的命名权限集的名称。

PolicyStatement
已过时.

获取或设置与代码组关联的策略语句。

(继承自 CodeGroup)

方法

AddChild(CodeGroup)
已过时.

将子代码组添加到当前代码组。

(继承自 CodeGroup)
Copy()
已过时.

创建当前代码组的深层副本。

CreateXml(SecurityElement, PolicyLevel)
已过时.

在派生类中重写时,序列化特定于派生代码组的属性和内部状态,并将序列化添加到指定的 SecurityElement

(继承自 CodeGroup)
Equals(CodeGroup, Boolean)
已过时.

确定指定的代码组是否等效于当前代码组,检查子代码组(如果指定)。

(继承自 CodeGroup)
Equals(Object)
已过时.

确定指定的代码组是否等效于当前代码组。

FromXml(SecurityElement, PolicyLevel)
已过时.

从 XML 编码重新构造具有给定状态和策略级别的安全对象。

(继承自 CodeGroup)
FromXml(SecurityElement)
已过时.

从 XML 编码重新构造具有给定状态的安全对象。

(继承自 CodeGroup)
GetHashCode()
已过时.

获取当前代码组的哈希代码。

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 (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)