HostSecurityManager HostSecurityManager HostSecurityManager HostSecurityManager Class

Definition

Ermöglicht die Steuerung und die Anpassung des Sicherheitsverhaltens für Anwendungsdomänen.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
Vererbung
HostSecurityManagerHostSecurityManagerHostSecurityManagerHostSecurityManager
Attribute

Beispiele

Das folgende Beispiel zeigt eine sehr einfache Implementierung von 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 'New


    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

Hinweise

AppDomainWenn Sie einen neuen erstellen, fragt der Common Language Runtime den AppDomainManager HostSecurityManagerauf das vorhanden sein eines ab, der an Sicherheitsentscheidungen für den AppDomainbeteiligt ist.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. Host Anbieter sollten einen Host Sicherheits-Manager implementieren, der von der HostSecurityManager -Klasse erbt.Host providers should implement a host security manager that inherits from the HostSecurityManager class.

Hinweise für Vererber

Einige Member eines HostSecurityManager werden immer dann aufgerufen, wenn eine Assembly entweder implizit oder explizit geladen wird.Some members of a HostSecurityManager are called whenever an assembly is loaded, either implicitly or explicitly. Die ProvideAssemblyEvidence(Assembly, Evidence) - ProvideAppDomainEvidence(Evidence) Methode und die-Methode dürfen keine Assemblys laden, da dies dazu führt, HostSecurityManager dass die Member der rekursiv aufgerufen werden.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. Um Zirkel Verweise zu vermeiden, sollten Sie neue Klassen Instanzen erstellen, die dazu führen können, dass Assemblys entweder implizit oder explizit im Konstruktor einer Klasse geladen werden, HostSecurityManagerdie von abgeleitet wird.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.

Konstruktoren

HostSecurityManager() HostSecurityManager() HostSecurityManager() HostSecurityManager()

Initialisiert eine neue Instanz der HostSecurityManager-Klasse.Initializes a new instance of the HostSecurityManager class.

Eigenschaften

DomainPolicy DomainPolicy DomainPolicy DomainPolicy

Ruft beim Überschreiben in einer abgeleiteten Klasse die Sicherheitsrichtlinie für die aktuelle Anwendungsdomäne ab.When overridden in a derived class, gets the security policy for the current application domain.

Flags Flags Flags Flags

Ruft das Flag ab, das die den Host betreffenden Komponenten für Sicherheitsrichtlinien darstellt.Gets the flag representing the security policy components of concern to the host.

Methoden

DetermineApplicationTrust(Evidence, Evidence, TrustManagerContext) DetermineApplicationTrust(Evidence, Evidence, TrustManagerContext) DetermineApplicationTrust(Evidence, Evidence, TrustManagerContext) DetermineApplicationTrust(Evidence, Evidence, TrustManagerContext)

Bestimmt, ob eine Anwendung ausgeführt werden soll.Determines whether an application should be executed.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Bestimmt, ob das angegebene Objekt mit dem aktuellen Objekt identisch ist.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GenerateAppDomainEvidence(Type) GenerateAppDomainEvidence(Type) GenerateAppDomainEvidence(Type) GenerateAppDomainEvidence(Type)

Fordert einen bestimmten Beweistyp für die Anwendungsdomäne an.Requests a specific evidence type for the application domain.

GenerateAssemblyEvidence(Type, Assembly) GenerateAssemblyEvidence(Type, Assembly) GenerateAssemblyEvidence(Type, Assembly) GenerateAssemblyEvidence(Type, Assembly)

Fordert einen bestimmten Beweistyp für die Assembly an.Requests a specific evidence type for the assembly.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Fungiert als Standardhashfunktion.Serves as the default hash function.

(Inherited from Object)
GetHostSuppliedAppDomainEvidenceTypes() GetHostSuppliedAppDomainEvidenceTypes() GetHostSuppliedAppDomainEvidenceTypes() GetHostSuppliedAppDomainEvidenceTypes()

Bestimmt, welchen Beweistyp der Host für die Anwendungsdomäne bereitstellen kann, wenn dies angefordert wird.Determines which evidence types the host can supply for the application domain, if requested.

GetHostSuppliedAssemblyEvidenceTypes(Assembly) GetHostSuppliedAssemblyEvidenceTypes(Assembly) GetHostSuppliedAssemblyEvidenceTypes(Assembly) GetHostSuppliedAssemblyEvidenceTypes(Assembly)

Bestimmt, welche Beweistypen der Host für die Assembly bereitstellen kann, wenn dies angefordert wird.Determines which evidence types the host can supply for the assembly, if requested.

GetType() GetType() GetType() GetType()

Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object.

(Inherited from Object)
ProvideAppDomainEvidence(Evidence) ProvideAppDomainEvidence(Evidence) ProvideAppDomainEvidence(Evidence) ProvideAppDomainEvidence(Evidence)

Stellt den Anwendungsdomänenbeweis für eine Assembly bereit, die derzeit geladen wird.Provides the application domain evidence for an assembly being loaded.

ProvideAssemblyEvidence(Assembly, Evidence) ProvideAssemblyEvidence(Assembly, Evidence) ProvideAssemblyEvidence(Assembly, Evidence) ProvideAssemblyEvidence(Assembly, Evidence)

Stellt den Assemblybeweis für eine Assembly bereit, die derzeit geladen wird.Provides the assembly evidence for an assembly being loaded.

ResolvePolicy(Evidence) ResolvePolicy(Evidence) ResolvePolicy(Evidence) ResolvePolicy(Evidence)

Bestimmt, welche Berechtigungen Code auf Basis der angegebenen Beweise gewährt werden.Determines what permissions to grant to code based on the specified evidence.

ToString() ToString() ToString() ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object.

(Inherited from Object)

Sicherheit

SecurityCriticalAttribute
erfordert volle Vertrauenswürdigkeit für den unmittelbaren Aufrufer.requires full trust for the immediate caller. Diese Klasse kann nicht von teilweise vertrauenswürdigem oder transparentem Code verwendet werden.This class cannot be used by partially trusted or transparent code.

InheritanceDemand
für vollständige Vertrauenswürdigkeit für Vererber.for full trust for inheritors. Diese Klasse kann von teilweise vertrauenswürdigem Code nicht geerbt werden.This class cannot be inherited by partially trusted code.

Gilt für: