Nasıl yapılır: Güvenlik Bağlamını İnceleme

Windows Communication Foundation (WCF) hizmetlerini programlama sırasında hizmet güvenlik bağlamı, hizmette kimlik doğrulaması yapmak için kullanılan istemci kimlik bilgileri ve talepleri hakkındaki ayrıntıları belirlemenize olanak tanır. Bu, ServiceSecurityContext sınıfının özellikleri kullanılarak yapılır.

Örneğin, PrimaryIdentity veya WindowsIdentity özelliğini kullanarak geçerli istemcinin kimliğini alabilirsiniz. İstemcinin anonim olup olmadığını belirlemek için özelliğini kullanın IsAnonymous .

Ayrıca, AuthorizationContext özelliğinde bulunan talep koleksiyonunu yineleyerek, istemci adına hangi taleplerin yapıldığını belirleyebilirsiniz.

Geçerli güvenlik bağlamını almak için

  • Geçerli güvenlik bağlamını almak için statik özelliğe Current erişin. Geçerli bağlamın referanstaki herhangi bir özelliğini inceleyin.

Çağıranın kimliğini belirlemek için

  1. PrimaryIdentity ve WindowsIdentity özelliklerinin değerini yazdırın.

Çağıranın taleplerini ayrıştırmak için

  1. Geçerli AuthorizationContext sınıfı döndürür. Current özelliğini kullanarak geçerli hizmet güvenlik bağlamını döndürün, ardından AuthorizationContext özelliğini kullanarak AuthorizationContext döndürün.

  2. ClaimSet sınıfının ClaimSets özelliği tarafından döndürülen AuthorizationContext nesne koleksiyonunu ayrıştırın.

Example

Aşağıdaki örnek, geçerli güvenlik bağlamının WindowsIdentity ve PrimaryIdentity özelliklerinin değerlerini, talebin kaynak değeri olarak ClaimType özelliğini ve geçerli güvenlik bağlamındaki her talebin Right özelliğini yazdırır.

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

Kodu Derleme

Kod aşağıdaki ad alanlarını kullanır:

Ayrıca bakınız