AppDomain.IsFullyTrusted Свойство
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает значение, указывающее, выполняются ли сборки, загруженные в текущий домен приложения с полным доверием.
public:
property bool IsFullyTrusted { bool get(); };
public bool IsFullyTrusted { get; }
member this.IsFullyTrusted : bool
Public ReadOnly Property IsFullyTrusted As Boolean
Значение свойства
Только .NET Framework: true если сборки, загруженные в текущий домен приложения, выполняются с полным доверием; в противном случае false.
.NET Core и .NET 5+: true во всех случаях.
Примеры
В следующем примере показано IsFullyTrusted свойство и Assembly.IsFullyTrusted свойство с полностью доверенными и частично доверенными доменами приложений. Полностью доверенный домен приложения — это домен приложения по умолчанию для приложения. Домен приложения с частичным доверием создается с помощью перегрузки AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) метода.
В примере используется класс, производный Worker от MarshalByRefObject, поэтому его можно маршалировать по границам домена приложения. В примере создается Worker объект в домене приложения по умолчанию. Затем он вызывает TestIsFullyTrusted метод для отображения значения свойства для домена приложения и для двух сборок, загруженных в домен приложения: mscorlib, который входит в .NET Framework и пример сборки. Домен приложения полностью доверенный, поэтому обе сборки полностью доверенны.
В примере создается другой Worker объект в изолированном домене приложения и снова вызывается TestIsFullyTrusted метод. Mscorlib всегда является доверенным, даже в частично доверенном домене приложения, но пример сборки частично доверяется.
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
*/
open System
open System.IO
open System.Security
open System.Security.Policy
type Worker() =
inherit MarshalByRefObject()
member _.TestIsFullyTrusted() =
let ad = AppDomain.CurrentDomain
printfn $"\nApplication domain '{ad.FriendlyName}': IsFullyTrusted = {ad.IsFullyTrusted}"
printfn $" IsFullyTrusted = {typeof<Worker>.Assembly.IsFullyTrusted} for the current assembly"
printfn $" IsFullyTrusted = {typeof<int>.Assembly.IsFullyTrusted} for mscorlib"
// ------------ Helper function ---------------------------------------
let getInternetSandbox () =
// Create the permission set to grant to all assemblies.
let hostEvidence = Evidence()
hostEvidence.AddHostEvidence(Zone System.Security.SecurityZone.Internet)
let pset = SecurityManager.GetStandardSandbox hostEvidence
// Identify the folder to use for the sandbox.
let ads = AppDomainSetup()
ads.ApplicationBase <- Directory.GetCurrentDirectory()
// Create the sandboxed application domain.
AppDomain.CreateDomain("Sandbox", hostEvidence, ads, pset, null)
let w = Worker()
w.TestIsFullyTrusted()
let adSandbox = getInternetSandbox()
let w2 =
adSandbox.CreateInstanceAndUnwrap(typeof<Worker>.Assembly.FullName, typeof<Worker>.FullName) :?> Worker
w2.TestIsFullyTrusted()
(* 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
'
Комментарии
Только .NET Framework: Этот метод всегда возвращается true для домена приложения по умолчанию приложения, работающего на рабочем столе. Он возвращается false для изолированного домена приложения, созданного с помощью AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) перегрузки метода, если разрешения, предоставленные домену приложения, эквивалентны полному доверию.