HostSecurityManager Classe

Definizione

Consente il controllo e la personalizzazione del comportamento di sicurezza per i domini applicazione.Allows the control and customization of security behavior for application domains.

public ref class HostSecurityManager
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
[System.Serializable]
public class HostSecurityManager
type HostSecurityManager = class
Public Class HostSecurityManager
Ereditarietà
HostSecurityManager
Attributi

Esempi

Nell'esempio seguente viene illustrata un'implementazione molto semplice di un HostSecurityManager.The following example shows a very simple implementation of a HostSecurityManager.

// To replace the default security manager with MySecurityManager, add the 
// assembly to the GAC and call MySecurityManager in the
// custom implementation of the AppDomainManager.

using System;
using System.Collections;
using System.Net;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Security.Principal;
using System.Threading;
using System.Runtime.InteropServices;
using System.Runtime.Hosting;

[assembly: System.Security.AllowPartiallyTrustedCallersAttribute()]
namespace MyNamespace
{
    [Serializable()]
    [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
    public class MySecurityManager : HostSecurityManager
    {
        public MySecurityManager()
        {
            Console.WriteLine(" Creating MySecurityManager.");
        }

        private HostSecurityManagerOptions hostFlags = HostSecurityManagerOptions.HostDetermineApplicationTrust |
                                                   HostSecurityManagerOptions.HostAssemblyEvidence;
        public override HostSecurityManagerOptions Flags
        {
            get
            {
                return hostFlags;
            }
        }

        public override Evidence ProvideAssemblyEvidence(Assembly loadedAssembly, Evidence evidence)
        {
            Console.WriteLine("Provide assembly evidence for: " + (loadedAssembly == null ? "Unknown" : loadedAssembly.ToString()) + ".");
            if (evidence == null)
                return null;

            evidence.AddAssemblyEvidence(new CustomEvidenceType());
            return evidence;
        }
        public override Evidence ProvideAppDomainEvidence(Evidence evidence)
        {
            Console.WriteLine("Provide evidence for the " + AppDomain.CurrentDomain.FriendlyName + " AppDomain.");
            if (evidence == null)
                return null;

            evidence.AddHostEvidence(new CustomEvidenceType());
            return evidence;
        }

        [SecurityPermissionAttribute(SecurityAction.Demand, Execution = true)]
        [SecurityPermissionAttribute(SecurityAction.Assert, Unrestricted = true)]
        public override ApplicationTrust DetermineApplicationTrust(Evidence applicationEvidence, Evidence activatorEvidence, TrustManagerContext context)
        {
            if (applicationEvidence == null)
                throw new ArgumentNullException("applicationEvidence");

            // Get the activation context from the application evidence.
            // This HostSecurityManager does not examine the activator evidence
            // nor is it concerned with the TrustManagerContext;
            // it simply grants the requested grant in the application manifest.

            IEnumerator enumerator = applicationEvidence.GetHostEnumerator();
            ActivationArguments activationArgs = null;
            while (enumerator.MoveNext())
            {
                activationArgs = enumerator.Current as ActivationArguments;
                if (activationArgs != null)
                    break;
            }

            if (activationArgs == null)
                return null;

            ActivationContext activationContext = activationArgs.ActivationContext;
            if (activationContext == null)
                return null;

            ApplicationTrust trust = new ApplicationTrust(activationContext.Identity);
            ApplicationSecurityInfo asi = new ApplicationSecurityInfo(activationContext);
            trust.DefaultGrantSet = new PolicyStatement(asi.DefaultRequestSet, PolicyStatementAttribute.Nothing);
            trust.IsApplicationTrustedToRun = true;
            return trust;
        }

    }
    [Serializable()]
    public class CustomEvidenceType : EvidenceBase
    {
        public CustomEvidenceType() { }

        public override string ToString()
        {
            return "CustomEvidenceType";
        }
    }
}
' To replace the default security manager with MySecurityManager, add the 
' assembly to the GAC and call MySecurityManager in the
' custom implementation of the AppDomainManager.
Imports System.Collections
Imports System.Net
Imports System.Reflection
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Policy
Imports System.Security.Principal
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Runtime.Hosting



<Assembly: System.Security.AllowPartiallyTrustedCallersAttribute()> 

<Serializable(), SecurityPermissionAttribute(SecurityAction.Demand, Flags:=SecurityPermissionFlag.Infrastructure)> _
Public Class MySecurityManager
    Inherits HostSecurityManager

    Public Sub New()
        Console.WriteLine(" Creating MySecurityManager.")

    End Sub


    Private hostFlags As HostSecurityManagerOptions = HostSecurityManagerOptions.HostDetermineApplicationTrust Or HostSecurityManagerOptions.HostAssemblyEvidence

    Public Overrides ReadOnly Property Flags() As HostSecurityManagerOptions
        Get
            Return hostFlags
        End Get
    End Property

    Public Overrides Function ProvideAssemblyEvidence(ByVal loadedAssembly As [Assembly], ByVal evidence As Evidence) As Evidence
        Console.WriteLine("Provide assembly evidence for: " + IIf(loadedAssembly Is Nothing, "Unknown", loadedAssembly.ToString()) + ".") 'TODO: For performance reasons this should be changed to nested IF statements
        If evidence Is Nothing Then
            Return Nothing
        End If
        evidence.AddAssemblyEvidence(New CustomEvidenceType())
        Return evidence

    End Function 'ProvideAssemblyEvidence

    Public Overrides Function ProvideAppDomainEvidence(ByVal evidence As Evidence) As Evidence
        Console.WriteLine("Provide evidence for the " + AppDomain.CurrentDomain.FriendlyName + " AppDomain.")
        If evidence Is Nothing Then
            Return Nothing
        End If
        evidence.AddHostEvidence(New CustomEvidenceType())
        Return evidence

    End Function 'ProvideAppDomainEvidence

    <SecurityPermissionAttribute(SecurityAction.Demand, Execution:=True), SecurityPermissionAttribute(SecurityAction.Assert, Unrestricted:=True)> _
    Public Overrides Function DetermineApplicationTrust(ByVal applicationEvidence As Evidence, ByVal activatorEvidence As Evidence, ByVal context As TrustManagerContext) As ApplicationTrust
        If applicationEvidence Is Nothing Then
            Throw New ArgumentNullException("applicationEvidence")
        End If
        ' Get the activation context from the application evidence.
        ' This HostSecurityManager does not examine the activator evidence
        ' nor is it concerned with the TrustManagerContext;
        ' it simply grants the requested grant in the application manifest.
        Dim enumerator As IEnumerator = applicationEvidence.GetHostEnumerator()
        Dim activationArgs As ActivationArguments = Nothing
        While enumerator.MoveNext()
            activationArgs = enumerator.Current '
            If Not (activationArgs Is Nothing) Then
                Exit While
            End If
        End While
        If activationArgs Is Nothing Then
            Return Nothing
        End If
        Dim activationContext As ActivationContext = activationArgs.ActivationContext
        If activationContext Is Nothing Then
            Return Nothing
        End If
        Dim trust As New ApplicationTrust(activationContext.Identity)
        Dim asi As New ApplicationSecurityInfo(activationContext)
        trust.DefaultGrantSet = New PolicyStatement(asi.DefaultRequestSet, PolicyStatementAttribute.Nothing)
        trust.IsApplicationTrustedToRun = True
        Return trust

    End Function 'DetermineApplicationTrust
End Class
<Serializable()> _
Public Class CustomEvidenceType
    Inherits EvidenceBase

    Public Sub New()

    End Sub

    Public Overrides Function ToString() As String
        Return "CustomEvidenceType"

    End Function 'ToString
End Class

Commenti

Quando si crea un nuovo AppDomain, il Common Language Runtime esegue una query sul AppDomainManager per la presenza di un HostSecurityManager, che fa parte della decisione sulla sicurezza per la AppDomain.When you create a new AppDomain, the common language runtime queries the AppDomainManager for the presence of a HostSecurityManager, which participates in making security decisions for the AppDomain. I provider host devono implementare un gestore della sicurezza host che eredita dalla classe HostSecurityManager.Host providers should implement a host security manager that inherits from the HostSecurityManager class.

Note per gli eredi

Alcuni membri di un HostSecurityManager vengono chiamati ogni volta che viene caricato un assembly, in modo implicito o esplicito.Some members of a HostSecurityManager are called whenever an assembly is loaded, either implicitly or explicitly. I metodi ProvideAssemblyEvidence(Assembly, Evidence) e ProvideAppDomainEvidence(Evidence) non devono caricare assembly, perché questa operazione comporterà la chiamata ricorsiva dei membri del HostSecurityManager.The ProvideAssemblyEvidence(Assembly, Evidence) and ProvideAppDomainEvidence(Evidence) methods must not load any assemblies, because doing so will result in the members of the HostSecurityManager being recursively called. Per evitare riferimenti circolari, è necessario creare nuove istanze delle classi che possono causare il caricamento degli assembly, in modo implicito o esplicito, nel costruttore di una classe che deriva da HostSecurityManager.To avoid circular references, you should create new instances of classes that can cause assemblies to be loaded, either implicitly or explicitly, in the constructor of a class that derives from HostSecurityManager.

Costruttori

HostSecurityManager()

Inizializza una nuova istanza della classe HostSecurityManager.Initializes a new instance of the HostSecurityManager class.

Proprietà

DomainPolicy

In caso di override in una classe derivata, ottiene il criterio di sicurezza per il dominio dell'applicazione corrente.When overridden in a derived class, gets the security policy for the current application domain.

Flags

Ottiene il flag che rappresenta i componenti dei criteri di sicurezza importanti per l'host.Gets the flag representing the security policy components of concern to the host.

Metodi

DetermineApplicationTrust(Evidence, Evidence, TrustManagerContext)

Determina se un'applicazione deve essere eseguita.Determines whether an application should be executed.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Ereditato da Object)
GenerateAppDomainEvidence(Type)

Richiede un tipo di evidenza specifico per il dominio applicazione.Requests a specific evidence type for the application domain.

GenerateAssemblyEvidence(Type, Assembly)

Richiede un tipo di evidenza specifico per l'assembly.Requests a specific evidence type for the assembly.

GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Ereditato da Object)
GetHostSuppliedAppDomainEvidenceTypes()

Determina i tipi di evidenza che possono essere forniti dall'host per il dominio applicazione, se necessario.Determines which evidence types the host can supply for the application domain, if requested.

GetHostSuppliedAssemblyEvidenceTypes(Assembly)

Determina i tipi di evidenza che possono essere forniti dall'host per l'assembly, se necessario.Determines which evidence types the host can supply for the assembly, if requested.

GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Ereditato da Object)
ProvideAppDomainEvidence(Evidence)

Fornisce l'evidenza del dominio applicazione del caricamento di un assembly.Provides the application domain evidence for an assembly being loaded.

ProvideAssemblyEvidence(Assembly, Evidence)

Fornisce l'evidenza del caricamento di un assembly.Provides the assembly evidence for an assembly being loaded.

ResolvePolicy(Evidence)

Determina le autorizzazioni da concedere al codice in base all'evidenza specificata.Determines what permissions to grant to code based on the specified evidence.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Ereditato da Object)

Sicurezza

SecurityCriticalAttribute
Richiede l'attendibilità totale per il chiamante immediato.requires full trust for the immediate caller. Questa classe non può essere utilizzata da codice parzialmente attendibile o trasparente.This class cannot be used by partially trusted or transparent code.

InheritanceDemand
per l'attendibilità totale per gli eredi.for full trust for inheritors. Questa classe non può essere ereditata da codice parzialmente attendibile.This class cannot be inherited by partially trusted code.

Si applica a