方法: セキュリティ コンテキストを調べる

Windows Communication Foundation (WCF) サービスのプログラミングを行う場合、サービス セキュリティ コンテキストを使用すると、サービスの認証で使用されるクライアントの資格情報とクレームの詳細を確認できます。 これは、ServiceSecurityContext クラスのプロパティを使用することで可能になります。

たとえば、PrimaryIdentity プロパティまたは WindowsIdentity プロパティを使用すると、現在のクライアントの ID を取得できます。 クライアントが匿名であるかどうかを確認するには、IsAnonymous プロパティを使用します。

また、AuthorizationContext プロパティにあるクレームのコレクションを反復処理することによって、クライアントのためにどのようなクレームが作成されているのかを確認することもできます。

現在のセキュリティ コンテキストを取得するには

  • 現在のセキュリティ コンテキストを取得するためには、静的プロパティ Current にアクセスします。 参照から現在のコンテキストの任意のプロパティを調べます。

呼び出し元の ID を確認するには

  1. PrimaryIdentity プロパティおよび WindowsIdentity プロパティの値を表示します。

呼び出し元のクレームを解析するには

  1. 現在の AuthorizationContext クラスを返します。 Current プロパティを使用して、現在のサービス セキュリティ コンテキストを返し、次に AuthorizationContext プロパティを使用して AuthorizationContext を返します。

  2. ClaimSet クラスの ClaimSets プロパティによって返された AuthorizationContext オブジェクトのコレクションを解析します。

次の例では、現在のセキュリティ コンテキストの WindowsIdentity プロパティおよび PrimaryIdentity プロパティの値、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

コードのコンパイル

コードでは、次の名前空間を使用します。

関連項目