Sdílet prostřednictvím


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é deklarace identity se provádějí jménem klienta iterací prostřednictvím kolekce deklarací ve AuthorizationContext vlastnosti.

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 PrimaryIdentity a WindowsIdentity vlastnosti.

Analýza deklarací identity volajícího

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

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

Příklad

Následující příklad vytiskne hodnoty WindowsIdentity a PrimaryIdentity vlastnosti aktuálního kontextu zabezpečení a ClaimType vlastnosti, hodnotu prostředku deklarace identity a Right vlastnost každé deklarace identity 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

Probíhá kompilace kódu

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

Viz také