Postupy: Prozkoumání kontextu zabezpečení

Při programování služeb WCF (Windows Communication Foundation) umožňuje kontext zabezpečení služby určit podrobnosti o přihlašovacích údajích klienta a deklarací identity použité k ověření ve službě. To se provádí pomocí vlastností ServiceSecurityContext třídy.

Identitu aktuálního klienta můžete například načíst pomocí PrimaryIdentity vlastnosti nebo WindowsIdentity vlastnosti. Chcete-li zjistit, zda je klient anonymní, použijte IsAnonymous vlastnost.

Můžete také určit, jaké nároky se provádějí jménem klienta iterací skrze kolekci nároků v AuthorizationContext atributu.

Získání aktuálního kontextu zabezpečení

  • Pokud chcete získat aktuální kontext zabezpečení, získejte přístup ke statické vlastnosti Current . Prozkoumejte některou z vlastností aktuálního kontextu z odkazu.

Určení identity volajícího

  1. Vytiskněte hodnotu vlastností PrimaryIdentity a WindowsIdentity.

Analýza tvrzení volajícího

  1. Vrátí aktuální třídu AuthorizationContext. Pomocí vlastnosti Current vrátíte aktuální kontext zabezpečení služby a potom vrátíte AuthorizationContext použitím vlastnosti AuthorizationContext.

  2. Analyzujte kolekci ClaimSet objektů vrácených ClaimSets vlastností AuthorizationContext třídy.

Příklad

Následující příklad vytiskne hodnoty vlastností WindowsIdentity a PrimaryIdentity aktuálního kontextu zabezpečení, vlastnost ClaimType, která je hodnota prostředku požadavku, a vlastnost Right každého požadavku v aktuálním kontextu zabezpečení.

// Run this method from within a method protected by the PrincipalPermissionAttribute
// to see the security context data, including the primary identity.
public void WriteServiceSecurityContextData(string fileName)
{
    using (StreamWriter sw = new StreamWriter(fileName))
    {
        // 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);
        sw.WriteLine();
        // 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}", claim.ClaimType);
                sw.WriteLine("\t Resource = {0}", claim.Resource.ToString());
                sw.WriteLine("\t Right = {0}", claim.Right);
            }
        }
    }
}

' Run this method from within a method protected by the PrincipalPermissionAttribute
' to see the security context data, including the primary identity.
Public Sub WriteServiceSecurityContextData(ByVal fileName As String)
    Dim sw As New StreamWriter(fileName)
    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)
        sw.WriteLine()
        ' 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}", claim.ClaimType)
                sw.WriteLine(vbTab + " Resource = {0}", claim.Resource.ToString())
                sw.WriteLine(vbTab + " Right = {0}", claim.Right)
            Next claim
        Next claimset
    Finally
        sw.Dispose()
    End Try

End Sub

Kompilace kódu

Kód používá následující obory názvů:

Viz také