방법: 보안 컨텍스트 검사

WCF(Windows Communication Foundation) 서비스를 프로그래밍할 때 서비스 보안 컨텍스트를 사용하면 서비스 인증에 사용되는 클라이언트 자격 증명 및 클레임에 대한 세부 정보를 확인할 수 있습니다. 이 작업은 클래스의 속성을 사용하여 수행됩니다 ServiceSecurityContext .

예를 들어, PrimaryIdentity 또는 WindowsIdentity 속성을 사용하여 현재 클라이언트의 ID를 조회할 수 있습니다. 클라이언트가 익명인지 여부를 확인하려면 속성을 사용합니다 IsAnonymous .

클레임 컬렉션을 AuthorizationContext 속성에서 반복하여 클라이언트를 대신하여 수행되는 클레임을 확인할 수도 있습니다.

현재 보안 컨텍스트를 얻으려면

  • 정적 속성 Current 에 액세스하여 현재 보안 컨텍스트를 가져옵니다. 참조에서 현재 컨텍스트의 속성을 검사합니다.

호출자의 ID를 확인하려면

  1. PrimaryIdentity 속성의 WindowsIdentity 값을 인쇄합니다.

호출자의 클레임을 분석하려면

  1. 현재 AuthorizationContext 클래스를 반환합니다. Current 속성을 사용하여 현재 서비스 보안 컨텍스트를 반환한 다음, AuthorizationContextAuthorizationContext 속성을 사용하여 반환합니다.

  2. 클래스 ClaimSetClaimSets 속성에서 반환된 AuthorizationContext 개체의 컬렉션을 구문 분석합니다.

예시

다음은 현재 보안 컨텍스트 및 속성의 WindowsIdentity 값과 PrimaryIdentityClaimType 속성, 클레임의 리소스 값 및 Right 현재 보안 컨텍스트에 있는 모든 클레임의 속성을 출력하는 예제입니다.

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

코드 컴파일

이 코드는 다음 네임스페이스를 사용합니다.

참고하십시오