Share via


ServiceSecurityContext クラス

定義

リモート パーティのセキュリティ コンテキストを表します。 クライアントではサービス ID を、サービスではクライアント ID を表します。

public ref class ServiceSecurityContext
public class ServiceSecurityContext
type ServiceSecurityContext = class
Public Class ServiceSecurityContext
継承
ServiceSecurityContext

次の例では、ServiceSecurityContext クラスを使用して、現在のセキュリティ コンテキストに関する情報を提供しています。 このコードは、StreamWriter クラスのインスタンスを作成して、情報をファイルに書き込みます。

// When this method runs, the caller must be an authenticated user
// and the ServiceSecurityContext is not a null instance.
public double Add(double n1, double n2)
{
    // Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    using (StreamWriter sw = new StreamWriter(@"c:\ServiceSecurityContextInfo.txt"))
    {
        // 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);

        // 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}, Resource: {1} Right: {2}",
                    claim.ClaimType,
                    claim.Resource.ToString(),
                    claim.Right);
                sw.WriteLine();
            }
        }
    }
    return n1 + n2;
}
' When this method runs, the caller must be an authenticated user and the ServiceSecurityContext 
' is not a null instance. 
Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
    ' Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    Dim sw As New StreamWriter("c:\ServiceSecurityContextInfo.txt")
    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)

        ' 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}, Resource: {1} Right: {2}", _
                claim.ClaimType, _
                claim.Resource.ToString(), _
                claim.Right)
                sw.WriteLine()
            Next claim
        Next claimset
    Finally
        sw.Dispose()
    End Try
    Return n1 + n2
End Function

次の例は、CheckAccessCore を使用してクレーム セットを解析する ServiceSecurityContext メソッドの実装を示しています。

public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        // Extract the action URI from the OperationContext. Match this against the claims
        // in the AuthorizationContext.
        string action = operationContext.RequestContext.RequestMessage.Headers.Action;
        Console.WriteLine("action: {0}", action);

        // Iterate through the various claimsets in the AuthorizationContext.
        foreach(ClaimSet cs in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
        {
            // Examine only those claim sets issued by System.
            if (cs.Issuer == ClaimSet.System)
            {
                // Iterate through claims of type "http://example.org/claims/allowedoperation".
                foreach (Claim c in cs.FindClaims("http://example.org/claims/allowedoperation",
                    Rights.PossessProperty))
                {
                    // Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString());

                    // If the Claim resource matches the action URI then return true to allow access.
                    if (action == c.Resource.ToString())
                        return true;
                }
            }
        }

        // If this point is reached, return false to deny access.
         return false;
    }
}
Public Class MyServiceAuthorizationManager
    Inherits ServiceAuthorizationManager
    
    Protected Overrides Function CheckAccessCore(ByVal operationContext As OperationContext) As Boolean 
        ' Extract the action URI from the OperationContext. Match this against the claims
        ' in the AuthorizationContext.
        Dim action As String = operationContext.RequestContext.RequestMessage.Headers.Action
        Console.WriteLine("action: {0}", action)
        
        ' Iterate through the various claimsets in the authorizationcontext.
        Dim cs As ClaimSet
        For Each cs In  operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets
            ' Examine only those claim sets issued by System.
            If cs.Issuer Is ClaimSet.System Then
                ' Iterate through claims of type "http://example.org/claims/allowedoperation".
                Dim c As Claim
                For Each c In  cs.FindClaims("http://example.org/claims/allowedoperation", _
                        Rights.PossessProperty)
                    ' Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString())
                    
                    ' If the Claim resource matches the action URI then return true to allow access.
                    If action = c.Resource.ToString() Then
                        Return True
                    End If
                Next c
            End If
        Next cs 
        ' If we get here, return false, denying access.
        Return False
    
    End Function 
End Class

注釈

このデータは、メッセージの SecurityMessageProperty の一部です。

実行時にリモート セキュリティ コンテキスト に関する情報を取得するには、このクラスを使用します。 クライアントが認証され、メソッドへのアクセスを承認されると、セキュリティ コンテキストが作成されます。 メッセージの認証および承認に成功すると、クライアントの現在のサービス インスタンスのセキュリティ情報を、このクラスのインスタンスから取得できます。

ServiceSecurityContext のインスタンスは、Current クラスの OperationContext プロパティから取得できます。または、次の例に示すように、サービス操作メソッドの中から使用できます。

ClaimSet の解析

このクラスは一般的に、メソッドにアクセスする際に、クライアントを識別するか承認することを目的として、現在のクレーム セットを取得するために使用します。 ClaimSet クラスは、Claim オブジェクトのコレクションを格納し、それぞれを解析して、特定のクレームが存在するかどうかを確認できます。 指定されたクレームが提供される場合は、承認を与えることができます。 この機能は、CheckAccessCore クラスの ServiceAuthorizationManager メソッドをオーバーライドすることで提供されます。 完全な例については、 承認ポリシーを参照してください。

状況によっては、リモート クライアントが匿名ユーザーとして認証される場合でも、IsAuthenticated インターフェイスの IIdentity プロパティが true を返すことに注意してください。 (このプロパティは PrimaryIdentity 、インターフェイスの実装を IIdentity 返します)。これが発生するには、次の状況が当てはまる必要があります。

  • サービスが Windows 認証を使用する。

  • サービスが、匿名ログオンを許可する。

  • バインディングは <customBinding> です。

  • カスタム バインドに <security> 要素が含まれている。

  • この <security> 要素には、属性が <. に設定された secureConversationBootstrap>requireSecurityContextCancellationfalseまれています。

コンストラクター

ServiceSecurityContext(AuthorizationContext)

指定した承認パラメーターを使用して、ServiceSecurityContext クラスの新しいインスタンスを初期化します。

ServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)

指定した承認パラメーターとポリシーのコレクションを使用して、ServiceSecurityContext クラスの新しいインスタンスを初期化します。

ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)

ポリシー オブジェクトのコレクションを使用して、ServiceSecurityContext クラスの新しいインスタンスを初期化します。

プロパティ

Anonymous

匿名パーティを表すために通常使用されるクレーム、ID、およびその他のコンテキスト データの空のコレクションを格納する ServiceSecurityContext クラスのインスタンスを返します。

AuthorizationContext

このクラスのインスタンスの承認情報を取得します。 AuthorizationContext は、アプリケーションがパーティの情報を問い合わせて取得できる ClaimSet のコレクションを格納します。

AuthorizationPolicies

このクラスのインスタンスに関連付けられたポリシーのコレクションを取得します。

Current

現在の ServiceSecurityContext を取得します。

IsAnonymous

現在のクライアントが、サービスに対する資格情報を提供しているかどうかを示す値を取得します。

PrimaryIdentity

現在の設定に関連付けられたプライマリ ID を取得します。

WindowsIdentity

現在の設定の Windows ID を取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください