AppDomain.IsFullyTrusted 属性

定义

获取一个值,该值指示加载到当前应用程序域的程序集是否是以完全信任方式执行的。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

属性值

Boolean

如果加载到当前应用程序域的程序集是以完全信任方式执行的,则为 true;否则为 falsetrue if assemblies that are loaded into the current application domain execute with full trust; otherwise, false.

示例

下面的示例演示 IsFullyTrusted Assembly.IsFullyTrusted 具有完全信任和部分信任的应用程序域的属性和属性。The following example demonstrates the IsFullyTrusted property and the Assembly.IsFullyTrusted property with fully trusted and partially trusted application domains. 完全受信任的应用程序域是应用程序的默认应用程序域。The fully trusted application domain is the default application domain for the application. 使用方法重载创建部分受信任的应用程序域 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.

该示例使用 Worker 派生自的类 MarshalByRefObject ,因此它可以跨应用程序域边界进行封送处理。The example uses a Worker class that derives from MarshalByRefObject, so it can be marshaled across application domain boundaries. 该示例 Worker 在默认应用程序域中创建对象。The example creates a Worker object in the default application domain. 然后,它调用 TestIsFullyTrusted 方法以显示应用程序域的属性值,以及加载到应用程序域中的两个程序集的属性值: mscorlib,这是 .NET Framework 的一部分,以及示例程序集。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. 应用程序域是完全受信任的,因此这两个程序集都是完全受信任的。The application domain is fully trusted, so both assemblies are fully trusted.

该示例 Worker 在沙盒应用程序域中创建另一个对象,并再次调用 TestIsFullyTrusted 方法。The example creates another Worker object in a sandboxed application domain and again calls the TestIsFullyTrusted method. 即使在部分受信任的应用程序域中,Mscorlib 也始终是受信任的,但该示例程序集是部分受信任的。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
' 

注解

此方法始终返回在 true 桌面上运行的应用程序的默认应用程序域。This method always returns true for the default application domain of an application that runs on the desktop. false 为使用方法重载创建的沙盒应用程序域返回 AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) ,除非授予该应用程序域的权限等效于完全信任。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.

适用于