ServiceSecurityContext Třída

Definice

Představuje kontext zabezpečení vzdálené strany. V klientovi představuje identitu služby a ve službě představuje identitu klienta.

public ref class ServiceSecurityContext
public class ServiceSecurityContext
type ServiceSecurityContext = class
Public Class ServiceSecurityContext
Dědičnost
ServiceSecurityContext

Příklady

Následující příklad používá ServiceSecurityContext třídu k poskytnutí informací o aktuálním kontextu zabezpečení. Kód vytvoří instanci StreamWriter třídy pro zápis informací do souboru.

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

Následující příklad ukazuje implementaci CheckAccessCore metody, která používá ServiceSecurityContext k analýze sady deklarací identity.

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

Poznámky

Data jsou součástí SecurityMessageProperty zprávy.

Tuto třídu použijte k získání informací o kontextu vzdáleného zabezpečení za běhu. Kontext zabezpečení se vytvoří, když se klient úspěšně ověří a autorizuje pro přístup k metodě. Pokud je zpráva úspěšně ověřena a autorizována, mohou být informace o zabezpečení z klienta a pro aktuální instanci služby získány z instance této třídy.

Můžete načíst instanci ServiceSecurityContext z Current vlastnosti OperationContext třídy nebo ji použít z metody operace služby, jak je znázorněno v následujícím příkladu.

Analýza sady deklarací identity

Běžným použitím třídy je načtení aktuální sady deklarací identity pro účely identifikace nebo autorizace klienta při přístupu k metodě. Třída ClaimSet obsahuje kolekci Claim objektů a každý z nich lze analyzovat a určit, zda je k dispozici konkrétní deklarace identity. Pokud je zadaná deklarace identity zadaná, můžete udělit autorizaci. Tato funkce je poskytována přepsáním CheckAccessCore metody ServiceAuthorizationManager třídy. Úplný příklad najdete v zásadách autorizace.

Mějte na paměti, že za určitých okolností vrátí IsAuthenticated vlastnost IIdentity rozhraní true , i když je vzdálený klient ověřen jako anonymní uživatel. (Vlastnost PrimaryIdentity vrátí implementaci IIdentity rozhraní.) Aby k tomu mohlo dojít, musí být splněny následující okolnosti:

  • Služba používá ověřování Windows.

  • Služba umožňuje anonymní přihlášení.

  • Vazba je <customBinding>.

  • Vlastní vazba obsahuje <security> prvek.

  • Element <security> obsahuje <secureConversationBootstrap> s requireSecurityContextCancellation atributem nastaveným na false.

Konstruktory

ServiceSecurityContext(AuthorizationContext)

Inicializuje novou instanci ServiceSecurityContext třídy se zadanými autorizačními parametry.

ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)

Inicializuje novou instanci ServiceSecurityContext třídy se zadanými parametry autorizace a kolekcí zásad.

ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)

Inicializuje novou instanci ServiceSecurityContext třídy s kolekcí zásad objektu.

Vlastnosti

Anonymous

Vrátí instanci ServiceSecurityContext třídy, která obsahuje prázdnou kolekci deklarací identity, identit a dalších kontextových dat, která se obvykle používají k reprezentaci anonymní strany.

AuthorizationContext

Získá autorizační informace pro instanci této třídy. Obsahuje AuthorizationContext kolekci ClaimSet , kterou může aplikace vyslechovat a načíst informace strany.

AuthorizationPolicies

Získá kolekci zásad přidružených k instanci této třídy.

Current

Získá aktuální ServiceSecurityContext.

IsAnonymous

Získá hodnotu, která označuje, zda aktuální klient zadal přihlašovací údaje ke službě.

PrimaryIdentity

Získá primární identitu přidruženou k aktuálnímu nastavení.

WindowsIdentity

Získá Windows identitu aktuálního nastavení.

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Type Získá aktuální instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří použádnou kopii aktuálního souboru Object.

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro

Viz také