AppDomain.IsFullyTrusted Propriété

Définition

Obtient une valeur qui indique si les assemblys chargés dans le domaine d'application actuel s'exécutent avec la confiance totale.Gets a value that indicates whether assemblies that are loaded into the current application domain execute with full trust.

public:
 property bool IsFullyTrusted { bool get(); };
public bool IsFullyTrusted { get; }
member this.IsFullyTrusted : bool
Public ReadOnly Property IsFullyTrusted As Boolean

Valeur de propriété

true si les assemblys chargés dans le domaine d'application actuel s'exécutent avec la confiance totale ; sinon, false.true if assemblies that are loaded into the current application domain execute with full trust; otherwise, false.

Exemples

L’exemple suivant illustre la propriété IsFullyTrusted et la propriété Assembly.IsFullyTrusted avec des domaines d’application d’un niveau de confiance total et d’un niveau de confiance partiel.The following example demonstrates the IsFullyTrusted property and the Assembly.IsFullyTrusted property with fully trusted and partially trusted application domains. Le domaine d’application de confiance totale est le domaine d’application par défaut de l’application.The fully trusted application domain is the default application domain for the application. Le domaine d’application de confiance partielle est créé à l’aide de la surcharge de méthode AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]).The partially trusted application domain is created by using the AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) method overload.

L’exemple utilise une classe Worker qui dérive de MarshalByRefObject, afin qu’elle puisse être marshalée au-delà des limites du domaine d’application.The example uses a Worker class that derives from MarshalByRefObject, so it can be marshaled across application domain boundaries. L’exemple crée un objet Worker dans le domaine d’application par défaut.The example creates a Worker object in the default application domain. Il appelle ensuite la méthode TestIsFullyTrusted pour afficher la valeur de la propriété pour le domaine d’application et pour deux assemblys qui sont chargés dans le domaine d’application : mscorlib, qui fait partie du .NET Framework et de l’exemple d’assembly.It then calls the TestIsFullyTrusted method to display the property value for the application domain and for two assemblies that are loaded into the application domain: mscorlib, which is part of the .NET Framework, and the example assembly. Le domaine d’application étant entièrement fiable, les deux assemblys sont entièrement fiables.The application domain is fully trusted, so both assemblies are fully trusted.

L’exemple crée un autre objet Worker dans un domaine d’application sandbox et appelle à nouveau la méthode TestIsFullyTrusted.The example creates another Worker object in a sandboxed application domain and again calls the TestIsFullyTrusted method. Mscorlib est toujours approuvé, même dans un domaine d’application de confiance partielle, mais l’exemple d’assembly est partiellement approuvé.Mscorlib is always trusted, even in a partially trusted application domain, but the example assembly is partially trusted.

using System;
 
namespace SimpleSandboxing
{
    public class Worker : MarshalByRefObject
    {
        static void Main()
        {
            Worker w = new Worker();
            w.TestIsFullyTrusted();

            AppDomain adSandbox = GetInternetSandbox();
            w = (Worker) adSandbox.CreateInstanceAndUnwrap(
                               typeof(Worker).Assembly.FullName,
                               typeof(Worker).FullName);
            w.TestIsFullyTrusted();
        }

        public void TestIsFullyTrusted()
        {
            AppDomain ad = AppDomain.CurrentDomain;
            Console.WriteLine("\r\nApplication domain '{0}': IsFullyTrusted = {1}",
                                        ad.FriendlyName, ad.IsFullyTrusted);

            Console.WriteLine("   IsFullyTrusted = {0} for the current assembly",
                             typeof(Worker).Assembly.IsFullyTrusted);

            Console.WriteLine("   IsFullyTrusted = {0} for mscorlib",
                                        typeof(int).Assembly.IsFullyTrusted);
        }

        // ------------ Helper method ---------------------------------------
        static AppDomain GetInternetSandbox()
        {
            // Create the permission set to grant to all assemblies.
            System.Security.Policy.Evidence hostEvidence = new System.Security.Policy.Evidence();
            hostEvidence.AddHostEvidence(new System.Security.Policy.Zone(
                                                         System.Security.SecurityZone.Internet));
            System.Security.PermissionSet pset = 
                                System.Security.SecurityManager.GetStandardSandbox(hostEvidence);

            // Identify the folder to use for the sandbox.
            AppDomainSetup ads = new AppDomainSetup();
            ads.ApplicationBase = System.IO.Directory.GetCurrentDirectory();
 
            // Create the sandboxed application domain.
            return AppDomain.CreateDomain("Sandbox", hostEvidence, ads, pset, null); 
        }
    }
}

/* This example produces output similar to the following:

Application domain 'Example.exe': IsFullyTrusted = True
   IsFullyTrusted = True for the current assembly
   IsFullyTrusted = True for mscorlib

Application domain 'Sandbox': IsFullyTrusted = False
   IsFullyTrusted = False for the current assembly
   IsFullyTrusted = True for mscorlib
 */
Public Class Worker
    Inherits MarshalByRefObject
    
    Shared Sub Main()
 
        Dim w As New Worker()
        w.TestIsFullyTrusted()
        
        Dim adSandbox As AppDomain = GetInternetSandbox()
        w = CType(adSandbox.CreateInstanceAndUnwrap(
                            GetType(Worker).Assembly.FullName, 
                            GetType(Worker).FullName), 
                  Worker)
        w.TestIsFullyTrusted()
    
    End Sub 
    
    Public Sub TestIsFullyTrusted() 

        Dim ad As AppDomain = AppDomain.CurrentDomain
        Console.WriteLine(vbCrLf & "Application domain '{0}': IsFullyTrusted = {1}", 
                          ad.FriendlyName, ad.IsFullyTrusted)
        
        Console.WriteLine("   IsFullyTrusted = {0} for the current assembly", 
                          GetType(Worker).Assembly.IsFullyTrusted)
        
        Console.WriteLine("   IsFullyTrusted = {0} for mscorlib", 
                          GetType(Integer).Assembly.IsFullyTrusted)
    
    End Sub 
    
    ' ------------ Helper method ---------------------------------------
    Shared Function GetInternetSandbox() As AppDomain 

        ' Create the permission set to grant to all assemblies.
        Dim hostEvidence As New System.Security.Policy.Evidence()
        hostEvidence.AddHostEvidence(
                    New System.Security.Policy.Zone(System.Security.SecurityZone.Internet))
        Dim pset As System.Security.PermissionSet = 
                           System.Security.SecurityManager.GetStandardSandbox(hostEvidence)
        
        ' Identify the folder to use for the sandbox.
        Dim ads As New AppDomainSetup()
        ads.ApplicationBase = System.IO.Directory.GetCurrentDirectory()
        
        ' Create the sandboxed application domain.
        Return AppDomain.CreateDomain("Sandbox", hostEvidence, ads, pset, Nothing)
    
    End Function 
End Class 

' This example produces output similar to the following:
'
'Application domain 'Example.exe': IsFullyTrusted = True
'   IsFullyTrusted = True for the current assembly
'   IsFullyTrusted = True for mscorlib
'
'Application domain 'Sandbox': IsFullyTrusted = False
'   IsFullyTrusted = False for the current assembly
'   IsFullyTrusted = True for mscorlib
' 

Remarques

Cette méthode retourne toujours true pour le domaine d’application par défaut d’une application qui s’exécute sur le bureau.This method always returns true for the default application domain of an application that runs on the desktop. Elle retourne false pour un domaine d’application bac à sable (sandbox) qui a été créé à l’aide de la surcharge de méthode AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]), à moins que les autorisations accordées au domaine d’application soient équivalentes à la confiance totale.It returns false for a sandboxed application domain that was created by using the AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) method overload, unless the permissions that are granted to the application domain are equivalent to full trust.

S’applique à