AppDomain.IsFullyTrusted Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém um valor que indica se assemblies carregados no domínio de aplicação atual são executados com total confiança.
public:
property bool IsFullyTrusted { bool get(); };
public bool IsFullyTrusted { get; }
member this.IsFullyTrusted : bool
Public ReadOnly Property IsFullyTrusted As Boolean
Valor de Propriedade
.NET Framework apenas: true se assemblies carregados no domínio de aplicação atual executam com total confiança; caso contrário, false.
.NET Core e .NET 5+: true em todos os casos.
Exemplos
O exemplo seguinte demonstra a IsFullyTrusted propriedade e a Assembly.IsFullyTrusted propriedade com domínios de aplicação totalmente confiáveis e parcialmente confiáveis. O domínio de aplicação totalmente confiável é o domínio de aplicação predefinido para a aplicação. O domínio de aplicação parcialmente confiável é criado usando a AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) sobrecarga de métodos.
O exemplo utiliza uma Worker classe que deriva de MarshalByRefObject, pelo que pode ser organizada através dos limites do domínio da aplicação. O exemplo cria um Worker objeto no domínio de aplicação padrão. Depois chama o método TestIsFullyTrusted para mostrar o valor da propriedade do domínio da aplicação e para dois assemblies que são carregados no domínio da aplicação: mscorlib, que faz parte do .NET Framework, e o assembly de exemplo. O domínio da aplicação é totalmente confiável, pelo que ambos os assemblies são totalmente confiáveis.
O exemplo cria outro Worker objeto num domínio de aplicação sandbox e volta a chamar o TestIsFullyTrusted método. O Mscorlib é sempre confiável, mesmo num domínio de aplicação parcialmente confiável, mas o assembly de exemplo é parcialmente confiável.
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
'
Observações
.NET Framework apenas: Este método devolve sempre true para o domínio de aplicação predefinido de uma aplicação que corre no ambiente de trabalho. Ele retorna false para um domínio de aplicação sandboxeado que foi criado usando a AppDomain.CreateDomain(String, Evidence, AppDomainSetup, PermissionSet, StrongName[]) sobrecarga de métodos, a menos que as permissões concedidas ao domínio da aplicação sejam equivalentes a confiança total.