İngilizce dilinde oku

Aracılığıyla paylaş


FileCodeGroup Sınıf

Tanım

Dikkat

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

Üyelik koşuluyla eşleşen kod derlemelerine kod derlemelerinde bulunan dosyaları işleme izni verir. Bu sınıf devralınamaz.

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
Devralma
FileCodeGroup
Öznitelikler

Örnekler

Aşağıdaki örnekte, FileCodeGroup sınıfının üyelerinin kullanımı gösterilmektedir.

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.

Açıklamalar

Dikkat

Kod Erişim Güvenliği (CAS), .NET Framework ve .NET'in tüm sürümlerinde kullanım dışı bırakılmıştır. .NET'in son sürümleri CAS ek açıklamalarını dikkate almaz ve CAS ile ilgili API'ler kullanılırsa hata üretir. Geliştiriciler, güvenlik görevlerini yerine getirmek için alternatif yöntemler aramalıdır.

Kod grupları, kod erişim güvenlik ilkesinin yapı taşlarıdır. Her ilke düzeyi, alt kod gruplarına sahip olabilecek bir kök kod grubundan oluşur. Her alt kod grubunun kendi alt kod grupları olabilir; bu davranış herhangi bir sayıda düzeye genişleterek bir ağaç oluşturur. Her kod grubu, belirli bir derlemenin bu derlemeye ait olup olmadığını bu derlemenin kanıtına göre belirleyen bir üyelik koşuluna sahiptir. Yalnızca üyelik koşulları belirli bir derlemeyle eşleşen kod grupları ve alt kod grupları ilke uygular.

FileCodeGroup, UnionCodeGroupile aynı alt eşleme semantiğine sahiptir. Ancak FileCodeGroup, kodun çalıştırıldığı dizine dosya erişimi veren dinamik olarak hesaplanan bir FileIOPermission içeren bir izin kümesi döndürür; UnionCodeGroup yalnızca statik bir izin kümesi döndürür. Verilen dosya erişimi türü oluşturucuya parametre olarak geçirilir.

Bu kod grubu yalnızca bir dosya protokolü üzerinden çalıştırılacak derlemelerle, yani bir dosyaya veya UNC yoluna işaret eden URL'lere sahip derlemelerle eşleşir.

Oluşturucular

FileCodeGroup(IMembershipCondition, FileIOPermissionAccess)
Geçersiz.

FileCodeGroup sınıfının yeni bir örneğini başlatır.

Özellikler

AttributeString
Geçersiz.

Kod grubu için ilke deyiminin özniteliklerinin dize gösterimini alır.

Children
Geçersiz.

Bir kod grubunun alt kod gruplarının sıralı listesini alır veya ayarlar.

(Devralındığı yer: CodeGroup)
Description
Geçersiz.

Kod grubunun açıklamasını alır veya ayarlar.

(Devralındığı yer: CodeGroup)
MembershipCondition
Geçersiz.

Kod grubunun üyelik koşulunu alır veya ayarlar.

(Devralındığı yer: CodeGroup)
MergeLogic
Geçersiz.

Birleştirme mantığını alır.

Name
Geçersiz.

Kod grubunun adını alır veya ayarlar.

(Devralındığı yer: CodeGroup)
PermissionSetName
Geçersiz.

Kod grubu için adlandırılmış izin kümesinin adını alır.

PolicyStatement
Geçersiz.

Kod grubuyla ilişkili ilke deyimini alır veya ayarlar.

(Devralındığı yer: CodeGroup)

Yöntemler

AddChild(CodeGroup)
Geçersiz.

Geçerli kod grubuna bir alt kod grubu ekler.

(Devralındığı yer: CodeGroup)
Copy()
Geçersiz.

Geçerli kod grubunun derin bir kopyasını oluşturur.

CreateXml(SecurityElement, PolicyLevel)
Geçersiz.

Türetilmiş bir sınıfta geçersiz kılındığında, türetilmiş bir kod grubuna özgü özellikleri ve iç durumu serileştirir ve serileştirmeyi belirtilen SecurityElementekler.

(Devralındığı yer: CodeGroup)
Equals(CodeGroup, Boolean)
Geçersiz.

Belirtilen kod grubunun geçerli kod grubuyla eşdeğer olup olmadığını belirler ve belirtilmişse alt kod gruplarını da denetler.

(Devralındığı yer: CodeGroup)
Equals(Object)
Geçersiz.

Belirtilen kod grubunun geçerli kod grubuyla eşdeğer olup olmadığını belirler.

FromXml(SecurityElement, PolicyLevel)
Geçersiz.

Belirli bir durum ve ilke düzeyine sahip bir güvenlik nesnesini XML kodlamasından yeniden oluşturur.

(Devralındığı yer: CodeGroup)
FromXml(SecurityElement)
Geçersiz.

Xml kodlamasından belirli bir duruma sahip bir güvenlik nesnesini yeniden oluşturur.

(Devralındığı yer: CodeGroup)
GetHashCode()
Geçersiz.

Geçerli kod grubunun karma kodunu alır.

GetType()
Geçersiz.

Geçerli örneğin Type alır.

(Devralındığı yer: Object)
MemberwiseClone()
Geçersiz.

Geçerli Objectbasit bir kopyasını oluşturur.

(Devralındığı yer: Object)
ParseXml(SecurityElement, PolicyLevel)
Geçersiz.

Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen SecurityElementtüretilmiş bir kod grubuna özgü özellikleri ve iç durumu yeniden oluşturur.

(Devralındığı yer: CodeGroup)
RemoveChild(CodeGroup)
Geçersiz.

Belirtilen alt kod grubunu kaldırır.

(Devralındığı yer: CodeGroup)
Resolve(Evidence)
Geçersiz.

Bir dizi kanıt için kod grubu ve alt öğeleri için ilkeyi çözümler.

ResolveMatchingCodeGroups(Evidence)
Geçersiz.

Eşleşen kod gruplarını çözümler.

ToString()
Geçersiz.

Geçerli nesneyi temsil eden bir dize döndürür.

(Devralındığı yer: Object)
ToXml()
Geçersiz.

Güvenlik nesnesinin ve geçerli durumunun XML kodlamasını oluşturur.

(Devralındığı yer: CodeGroup)
ToXml(PolicyLevel)
Geçersiz.

Güvenlik nesnesinin, geçerli durumunun ve kodun bulunduğu ilke düzeyinin XML kodlamasını oluşturur.

(Devralındığı yer: CodeGroup)

Şunlara uygulanır

Ürün Sürümler (Kullanım dışı)
.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)