Language

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

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

コードのコンパイル

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

こちらも参照ください