ServiceSecurityContext Klasse

Definition

Stellt den Sicherheitskontext eines Remote-Teilnehmers dar. Stellt auf dem Client die Dienstidentität und 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 über den 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

Im folgenden Beispiel wird eine Implementierung der CheckAccessCore-Methode dargestellt, die ServiceSecurityContext verwendet, um einen Anspruchssatz zu analysieren.

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 von SecurityMessageProperty für eine Nachricht.

Verwenden Sie diese Klasse, um Informationen zu einem Remotesicherheitskontext zur Laufzeit abzurufen. Ein Sicherheitskontext wird erstellt, wenn ein Client erfolgreich authentifiziert wird und die Berechtigung erhält, auf eine Methode zuzugreifen. Wenn eine Nachricht erfolgreich authentifiziert wurde und die entsprechenden Berechtigungen erhalten hat, können die Sicherheitsinformationen vom Client und für die aktuelle Dienstinstanz von einer Instanz dieser Klasse abgerufen werden.

Sie können eine Instanz von ServiceSecurityContext aus der Current-Eigenschaft der OperationContext-Klasse abrufen oder sie wie im folgenden Beispiel dargestellt in einer Dienstvorgangsmethode verwenden.

Analysieren eines ClaimSet

Eine übliche Verwendung dieser Klasse ist das Abrufen des aktuellen Satzes an Ansprüchen, um einen Client beim Zugreifen auf eine Methode zu identifizieren oder zu autorisieren. Die ClaimSet-Klasse enthält eine Auflistung von Claim-Objekten und jedes kann analysiert werden, um zu bestimmen, ob ein bestimmter Anspruch vorliegt. Wenn der angegebene Anspruch vorliegt, kann die Autorisierung gewährt werden. Diese Funktion wird bereitgestellt, indem die CheckAccessCore-Methode der ServiceAuthorizationManager-Klasse überschrieben wird. Ein vollständiges Beispiel finden Sie in der Autorisierungsrichtlinie.

Beachten Sie, dass unter einigen Bedingungen die IsAuthenticated-Eigenschaft der IIdentity-Schnittstelle den Wert true zurückgibt, selbst wenn der Remote-Client als anonymer Benutzer authentifiziert wurde. (Die PrimaryIdentity Eigenschaft gibt eine Implementierung der IIdentity Schnittstelle zurück.) Die folgenden Umstände müssen erfüllt sein, damit dies geschehen kann:

  • Der Dienst verwendet die Windows-Authentifizierung.

  • Der Dienst ermöglicht anonyme Anmeldungen.

  • Die Bindung ist ein <customBinding>.

  • Die benutzerdefinierte Bindung umfasst ein <security>-Element.

  • Das <security> Element enthält einen <secureConversationBootstrap> mit dem requireSecurityContextCancellation Attributsatz auf false.

Konstruktoren

ServiceSecurityContext(AuthorizationContext)

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

ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)

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

ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)

Initialisiert eine neue Instanz der ServiceSecurityContext-Klasse mit der Auflistung der Richtlinienobjekte.

Eigenschaften

Anonymous

Gibt eine Instanz der ServiceSecurityContext-Klasse zurück, die eine leere Liste mit Ansprüchen, Identitäten und anderen Kontextinformationen enthält, die normalerweise zur Darstellung eines anonymen Teilnehmers verwendet wird.

AuthorizationContext

Ruft die Autorisierungsinformationen für eine Instanz dieser Klasse ab. AuthorizationContext enthält eine Auflistung von ClaimSet, die die Anwendung abfragen kann. Darüber hinaus werden die Informationen des Teilnehmers abgerufen.

AuthorizationPolicies

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

Current

Ruft den aktuellen ServiceSecurityContext ab.

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

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ToString()

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

(Geerbt von Object)

Gilt für

Siehe auch