Freigeben über


ServiceSecurityContext Klasse

Definition

Stellt den Sicherheitskontext einer Remotepartei dar. Stellt auf dem Client die Dienstidentität dar und stellt im Dienst die Clientidentität dar.

public ref class ServiceSecurityContext
public class ServiceSecurityContext
type ServiceSecurityContext = class
Public Class ServiceSecurityContext
Vererbung
ServiceSecurityContext

Beispiele

Im folgenden Beispiel wird die ServiceSecurityContext Klasse verwendet, um Informationen zum aktuellen Sicherheitskontext bereitzustellen. Der Code erstellt eine Instanz der StreamWriter Klasse, um die Informationen in eine Datei zu schreiben.

// When this method runs, the caller must be an authenticated user
// and the ServiceSecurityContext is not a null instance.
public double Add(double n1, double n2)
{
    // Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    using (StreamWriter sw = new StreamWriter(@"c:\ServiceSecurityContextInfo.txt"))
    {
        // Write the primary identity and Windows identity. The primary identity is derived from
        // the credentials used to authenticate the user. The Windows identity may be a null string.
        sw.WriteLine("PrimaryIdentity: {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);
        sw.WriteLine("WindowsIdentity: {0}", ServiceSecurityContext.Current.WindowsIdentity.Name);

        // Write the claimsets in the authorization context. By default, there is only one claimset
        // provided by the system.
        foreach (ClaimSet claimset in ServiceSecurityContext.Current.AuthorizationContext.ClaimSets)
        {
            foreach (Claim claim in claimset)
            {
                // Write out each claim type, claim value, and the right. There are two
                // possible values for the right: "identity" and "possessproperty".
                sw.WriteLine("Claim Type: {0}, Resource: {1} Right: {2}",
                    claim.ClaimType,
                    claim.Resource.ToString(),
                    claim.Right);
                sw.WriteLine();
            }
        }
    }
    return n1 + n2;
}
' When this method runs, the caller must be an authenticated user and the ServiceSecurityContext 
' is not a null instance. 
Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
    ' Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    Dim sw As New StreamWriter("c:\ServiceSecurityContextInfo.txt")
    Try
        ' Write the primary identity and Windows identity. The primary identity is derived from 
        ' the credentials used to authenticate the user. The Windows identity may be a null string.
        sw.WriteLine("PrimaryIdentity: {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name)
        sw.WriteLine("WindowsIdentity: {0}", ServiceSecurityContext.Current.WindowsIdentity.Name)

        ' Write the claimsets in the authorization context. By default, there is only one claimset
        ' provided by the system. 
        Dim claimset As ClaimSet
        For Each claimset In ServiceSecurityContext.Current.AuthorizationContext.ClaimSets
            Dim claim As Claim
            For Each claim In claimset
                ' Write out each claim type, claim value, and the right. There are two
                ' possible values for the right: "identity" and "possessproperty". 
                sw.WriteLine("Claim Type: {0}, Resource: {1} Right: {2}", _
                claim.ClaimType, _
                claim.Resource.ToString(), _
                claim.Right)
                sw.WriteLine()
            Next claim
        Next claimset
    Finally
        sw.Dispose()
    End Try
    Return n1 + n2
End Function

Das folgende Beispiel zeigt eine Implementierung der CheckAccessCore Methode, die zum ServiceSecurityContext Analysieren einer Gruppe von Ansprüchen verwendet wird.

public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        // Extract the action URI from the OperationContext. Match this against the claims
        // in the AuthorizationContext.
        string action = operationContext.RequestContext.RequestMessage.Headers.Action;
        Console.WriteLine("action: {0}", action);

        // Iterate through the various claimsets in the AuthorizationContext.
        foreach(ClaimSet cs in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
        {
            // Examine only those claim sets issued by System.
            if (cs.Issuer == ClaimSet.System)
            {
                // Iterate through claims of type "http://example.org/claims/allowedoperation".
                foreach (Claim c in cs.FindClaims("http://example.org/claims/allowedoperation",
                    Rights.PossessProperty))
                {
                    // Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString());

                    // If the Claim resource matches the action URI then return true to allow access.
                    if (action == c.Resource.ToString())
                        return true;
                }
            }
        }

        // If this point is reached, return false to deny access.
         return false;
    }
}
Public Class MyServiceAuthorizationManager
    Inherits ServiceAuthorizationManager
    
    Protected Overrides Function CheckAccessCore(ByVal operationContext As OperationContext) As Boolean 
        ' Extract the action URI from the OperationContext. Match this against the claims
        ' in the AuthorizationContext.
        Dim action As String = operationContext.RequestContext.RequestMessage.Headers.Action
        Console.WriteLine("action: {0}", action)
        
        ' Iterate through the various claimsets in the authorizationcontext.
        Dim cs As ClaimSet
        For Each cs In  operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets
            ' Examine only those claim sets issued by System.
            If cs.Issuer Is ClaimSet.System Then
                ' Iterate through claims of type "http://example.org/claims/allowedoperation".
                Dim c As Claim
                For Each c In  cs.FindClaims("http://example.org/claims/allowedoperation", _
                        Rights.PossessProperty)
                    ' Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString())
                    
                    ' If the Claim resource matches the action URI then return true to allow access.
                    If action = c.Resource.ToString() Then
                        Return True
                    End If
                Next c
            End If
        Next cs 
        ' If we get here, return false, denying access.
        Return False
    
    End Function 
End Class

Hinweise

Die Daten sind Teil der SecurityMessageProperty Nachricht.

Verwenden Sie diese Klasse, um Informationen zu einem Remotesicherheitskontext zur Laufzeit abzurufen. Ein Sicherheitskontext wird erstellt, wenn ein Client erfolgreich authentifiziert und für den Zugriff auf eine Methode autorisiert wurde. Wenn eine Nachricht erfolgreich authentifiziert und autorisiert wird, können die Sicherheitsinformationen vom Client und für die aktuelle Dienstinstanz aus einer Instanz dieser Klasse abgerufen werden.

Sie können eine Instanz der ServiceSecurityContext Von der Current Eigenschaft der OperationContext Klasse abrufen oder sie aus einer Dienstoperationsmethode verwenden, wie im folgenden Beispiel gezeigt.

Analysieren eines ClaimSets

Eine häufige Verwendung der Klasse besteht darin, die aktuelle Gruppe von Ansprüchen abzurufen, um einen Client beim Zugriff auf eine Methode zu identifizieren oder zu autorisieren. Die ClaimSet Klasse enthält eine Auflistung von Claim Objekten, und jede kann analysiert werden, um zu bestimmen, ob ein bestimmter Anspruch vorhanden ist. Wenn der angegebene Anspruch angegeben wird, kann die Autorisierung erteilt werden. Diese Funktionalität wird durch Überschreiben der CheckAccessCore Methode der ServiceAuthorizationManager Klasse bereitgestellt. Ein vollständiges Beispiel finden Sie in der Autorisierungsrichtlinie.

Beachten Sie, dass die IsAuthenticated Eigenschaft der IIdentity Schnittstelle unter bestimmten Umständen auch dann zurückgegeben true wird, wenn der Remoteclient als anonymer Benutzer authentifiziert wird. (Die PrimaryIdentity Eigenschaft gibt eine Implementierung der IIdentity Schnittstelle zurück.) Die folgenden Umstände müssen erfüllt sein, damit dies geschieht:

  • Der Dienst verwendet die Windows-Authentifizierung.

  • Der Dienst ermöglicht anonyme Anmeldungen.

  • Die Bindung ist ein <customBinding>.

  • Die benutzerdefinierte Bindung enthält ein <security> Element.

  • Das <security> Element enthält einen <secureConversationBootstrap> mit dem Attribut, auf falsedas requireSecurityContextCancellation festgelegt ist.

Konstruktoren

Name Beschreibung
ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)

Initialisiert eine neue Instanz der ServiceSecurityContext Klasse mit den angegebenen Autorisierungsparametern und der Auflistung von Richtlinien.

ServiceSecurityContext(AuthorizationContext)

Initialisiert eine neue Instanz der ServiceSecurityContext Klasse mit den angegebenen Autorisierungsparametern.

ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)

Initialisiert eine neue Instanz der ServiceSecurityContext Klasse mit der Auflistung des Richtlinienobjekts.

Eigenschaften

Name Beschreibung
Anonymous

Gibt eine Instanz der Klasse zurück, die ServiceSecurityContext eine leere Auflistung von Ansprüchen, Identitäten und anderen Kontextdaten enthält, die normalerweise zur Darstellung einer anonymen Partei verwendet werden.

AuthorizationContext

Ruft die Autorisierungsinformationen für eine Instanz dieser Klasse ab. Die AuthorizationContext enthält eine Sammlung ClaimSet davon, dass die Anwendung die Informationen der Partei abfragen und abrufen kann.

AuthorizationPolicies

Ruft die Auflistung von Richtlinien ab, die einer Instanz dieser Klasse zugeordnet sind.

Current

Ruft den aktuellen ServiceSecurityContext.

IsAnonymous

Ruft einen Wert ab, der angibt, ob der aktuelle Client Anmeldeinformationen für den Dienst bereitgestellt hat.

PrimaryIdentity

Ruft die primäre Identität ab, die der aktuellen Einstellung zugeordnet ist.

WindowsIdentity

Ruft die Windows-Identität der aktuellen Einstellung ab.

Methoden

Name Beschreibung
Equals(Object)

Bestimmt, ob das angegebene Objekt dem aktuellen Objekt entspricht.

(Geerbt von Object)
GetHashCode()

Dient als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft die Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie der aktuellen Object.

(Geerbt von Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Gilt für:

Weitere Informationen