Bagikan melalui


HostSecurityManager Kelas

Definisi

Memungkinkan kontrol dan kustomisasi perilaku keamanan untuk domain aplikasi.

public ref class HostSecurityManager
public class HostSecurityManager
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class HostSecurityManager
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
[System.Security.SecurityCritical]
public class HostSecurityManager
type HostSecurityManager = class
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type HostSecurityManager = class
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
[<System.Security.SecurityCritical>]
type HostSecurityManager = class
Public Class HostSecurityManager
Warisan
HostSecurityManager
Atribut

Contoh

Contoh berikut menunjukkan implementasi yang HostSecurityManagersangat sederhana dari .

// 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

Keterangan

Saat Anda membuat yang baru AppDomain, runtime bahasa umum mengkueri AppDomainManager keberadaan HostSecurityManager, yang berpartisipasi dalam membuat keputusan keamanan untuk AppDomain. Penyedia host harus menerapkan manajer keamanan host yang mewarisi dari HostSecurityManager kelas .

Catatan Bagi Inheritor

Beberapa anggota dipanggil HostSecurityManager setiap kali assembly dimuat, baik secara implisit maupun eksplisit. Metode ProvideAssemblyEvidence(Assembly, Evidence) dan ProvideAppDomainEvidence(Evidence) tidak boleh memuat rakitan apa pun, karena melakukannya akan mengakibatkan anggota HostSecurityManager yang dipanggil secara rekursif. Untuk menghindari referensi melingkar, Anda harus membuat instans baru kelas yang dapat menyebabkan rakitan dimuat, baik secara implisit maupun eksplisit, di konstruktor kelas yang berasal dari HostSecurityManager.

Konstruktor

HostSecurityManager()

Menginisialisasi instans baru kelas HostSecurityManager.

Properti

DomainPolicy
Kedaluwarsa.

Ketika ditimpa di kelas turunan, mendapatkan kebijakan keamanan untuk domain aplikasi saat ini.

Flags

Mendapatkan bendera yang mewakili komponen kebijakan keamanan yang menjadi perhatian host.

Metode

DetermineApplicationTrust(Evidence, Evidence, TrustManagerContext)

Menentukan apakah aplikasi harus dijalankan.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GenerateAppDomainEvidence(Type)

Meminta jenis bukti tertentu untuk domain aplikasi.

GenerateAssemblyEvidence(Type, Assembly)

Meminta jenis bukti tertentu untuk perakitan.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetHostSuppliedAppDomainEvidenceTypes()

Menentukan jenis bukti mana yang dapat disediakan host untuk domain aplikasi, jika diminta.

GetHostSuppliedAssemblyEvidenceTypes(Assembly)

Menentukan jenis bukti mana yang dapat disediakan host untuk perakitan, jika diminta.

GetType()

Mendapatkan instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari yang saat ini Object.

(Diperoleh dari Object)
ProvideAppDomainEvidence(Evidence)

Menyediakan bukti domain aplikasi untuk perakitan yang dimuat.

ProvideAssemblyEvidence(Assembly, Evidence)

Menyediakan bukti perakitan untuk perakitan yang dimuat.

ResolvePolicy(Evidence)
Kedaluwarsa.

Menentukan izin apa yang akan diberikan ke kode berdasarkan bukti yang ditentukan.

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk