방법: 보안 컨텍스트 검사

WCF(Windows Communication Foundation) 서비스를 프로그래밍하는 경우 서비스 보안 컨텍스트를 사용하여 서비스를 인증하는 데 사용된 클라이언트 자격 증명 및 클레임에 대한 자세한 내용을 확인할 수 있습니다. ServiceSecurityContext 클래스의 속성을 사용하여 이를 수행할 수 있습니다.

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

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

현재 보안 컨텍스트를 가져오려면

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

호출자의 ID를 확인하려면

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

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

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

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

예시

다음 예제에서는 현재 보안 컨텍스트의 WindowsIdentityPrimaryIdentity 속성에 대한 값, ClaimType 속성, 클레임의 리소스 값 및 현재 보안 컨텍스트의 모든 클레임에 대한 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

코드 컴파일

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

참고 항목