方法: カスタム クレームを作成する

Windows Communication Foundation (WCF) の ID モデル インフラストラクチャでは、一連の組み込みクレームの種類と権限、およびその種類と権限を使用して Claim インスタンスを作成するヘルパー関数が提供されます。 この組み込みクレームは、WCF が既定でサポートするクライアント資格情報の型内にある情報をモデル化するように作成されています。 多くの場合はこの組み込みクレームで十分ですが、一部のアプリケーションでカスタム クレームが必要になる場合があります。 クレームは、クレームが適用されるリソースを示すクレームの種類と、リソースにアサートされる権限で構成されます。 このトピックでは、カスタム クレームを作成する方法について説明します。

プリミティブ データ型に基づくカスタム クレームを作成するには

  1. クレームの種類、リソースの値、Claim(String, Object, String) コンストラクターへの権限を渡すことで、カスタム クレームを作成します。

    1. クレームの種類の一意の値を指定します。

      クレームの種類は一意の文字列識別子です。 カスタム クレームを作成する場合、クレームの種類に使用されている文字列識別子が一意になるようにしてください。 WCF によって定義されたクレームの種類の一覧については、ClaimTypes クラスを参照してください。

    2. プリミティブ データ型とリソースの値を選択します。

      リソースはオブジェクトです。 CLR 型のリソースにはプリミティブを指定できます。たとえば、StringInt32、または任意のシリアル化可能な型を指定できます。 クレームは WCF によりさまざまな点でシリアル化されるため、CLR 型のリソースはシリアル化可能なものである必要があります。 プリミティブ型はシリアル化できます。

    3. WCF で定義されている権限、またはカスタム権限の一意の値を選択します。

      権限は一意の文字列識別子です。 WCF で定義された権限は Rights クラスで定義されています。

      カスタム クレームを作成する場合、権限に使用されている文字列識別子が一意になるようにしてください。

      次のコード例では、http://example.org/claims/simplecustomclaim という名前のリソース用の Driver's License というクレームの種類と PossessProperty 権限を持つカスタム クレームを作成します。

    // Create claim with custom claim type and primitive resource
    Claim c1 = new Claim ( "http://example.org/claims/simplecustomclaim", "Driver's License", Rights.PossessProperty);
    
    ' Create claim with custom claim type and primitive resource
    Dim c1 As New Claim("http://example.org/claims/simplecustomclaim", "Driver's License", Rights.PossessProperty)
    

プリミティブ以外のデータ型に基づくカスタム クレームを作成するには

  1. クレームの種類、リソースの値、Claim(String, Object, String) コンストラクターへの権限を渡すことで、カスタム クレームを作成します。

    1. クレームの種類の一意の値を指定します。

      クレームの種類は一意の文字列識別子です。 カスタム クレームを作成する場合、クレームの種類に使用されている文字列識別子が一意になるようにしてください。 WCF によって定義されたクレームの種類の一覧については、ClaimTypes クラスを参照してください。

    2. リソース用のシリアル化可能な、プリミティブ型以外の型を選択または定義します。

      リソースはオブジェクトです。 クレームは WCF によりさまざまな点でシリアル化されるため、CLR 型のリソースはシリアル化可能なものである必要があります。 プリミティブ型は既にシリアル化できます。

      新しい型を作成する場合は、DataContractAttribute をクラスに適用します。 また、クレームの一部としてシリアル化する必要のある新しい型のすべてのメンバーにも DataMemberAttribute 属性を適用します。

      MyResourceType というカスタム リソース型を定義するコード例を次に示します。

      [DataContract(Name="MyResource", Namespace="http://example.org/resources")]
      public sealed class MyResourceType
      {
        // private members
        private string text;
        private int number;
      
        // Constructors
        public MyResourceType()
        {
        }
      
        public MyResourceType(string text, int number )
        {
          this.text = text;
          this.number = number;
        }
      
        // Public properties
        [DataMember]
        public string Text { get { return this.text; }  set { this.text = value; } }
        [DataMember]
        public int Number { get { return this.number; } set { this.number = value; } }
      }
      
      <DataContract(Name:="MyResource", [Namespace]:="http://example.org/resources")> _
      NotInheritable Public Class MyResourceType
          ' private members
          Private text_value As String
          Private number_value As Integer
      
      
          ' Constructors
          Public Sub New()
      
          End Sub
      
      
          Public Sub New(ByVal text As String, ByVal number As Integer)
              Me.text_value = text
              Me.number = number
      
          End Sub
      
          ' Public properties
      
          <DataMember()> _
          Public Property Text() As String
              Get
                  Return Me.text_value
              End Get
              Set
                  Me.text_value = value
              End Set
          End Property
      
          <DataMember()> _
          Public Property Number() As Integer
              Get
                  Return Me.number_value
              End Get
              Set
                  Me.number_value = value
              End Set
          End Property
      End Class
      
    3. WCF で定義されている権限、またはカスタム権限の一意の値を選択します。

      権限は一意の文字列識別子です。 WCF で定義された権限は Rights クラスで定義されています。

      カスタム クレームを作成する場合、権限に使用されている文字列識別子が一意になるようにしてください。

      次のコード例では、http://example.org/claims/complexcustomclaim というクレームの種類 (MyResourceType カスタム リソース型) および PossessProperty 権限を持つカスタム クレームを作成します。

      // Create claim with custom claim type and structured resource type
      Claim c2 = new Claim ( "http://example.org/claims/complexcustomclaim", new MyResourceType ( "Martin", 38 ), Rights.PossessProperty);
      
      ' Create claim with custom claim type and structured resource type
      Dim c2 As New Claim("http://example.org/claims/complexcustomclaim", New MyResourceType("Martin", 38), Rights.PossessProperty)
      

次のコード例で、プリミティブ リソース型を持つカスタム クレームと、プリミティブ以外のリソース型を持つカスタム クレームの作成方法を示します。

using System;
using System.IdentityModel.Claims;
using System.Runtime.Serialization;

namespace Samples
{
  [DataContract(Name="MyResource", Namespace="http://example.org/resources")]
  public sealed class MyResourceType
  {
    // private members
    private string text;
    private int number;

    // Constructors
    public MyResourceType()
    {
    }

    public MyResourceType(string text, int number )
    {
      this.text = text;
      this.number = number;
    }

    // Public properties
    [DataMember]
    public string Text { get { return this.text; }  set { this.text = value; } }
    [DataMember]
    public int Number { get { return this.number; } set { this.number = value; } }
  }

  class Program
  {
    public static void Main()
    {
      // Create claim with custom claim type and primitive resource
      Claim c1 = new Claim ( "http://example.org/claims/simplecustomclaim", "Driver's License", Rights.PossessProperty);
      // Create claim with custom claim type and structured resource type
      Claim c2 = new Claim ( "http://example.org/claims/complexcustomclaim", new MyResourceType ( "Martin", 38 ), Rights.PossessProperty);

      // Do something with claims
    }
  }
}
Imports System.IdentityModel.Claims
Imports System.Runtime.Serialization
Imports System.Security.Permissions



<DataContract(Name:="MyResource", [Namespace]:="http://example.org/resources")> _
NotInheritable Public Class MyResourceType
    ' private members
    Private text_value As String
    Private number_value As Integer


    ' Constructors
    Public Sub New()

    End Sub


    Public Sub New(ByVal text As String, ByVal number As Integer)
        Me.text_value = text
        Me.number = number

    End Sub

    ' Public properties

    <DataMember()> _
    Public Property Text() As String
        Get
            Return Me.text_value
        End Get
        Set
            Me.text_value = value
        End Set
    End Property

    <DataMember()> _
    Public Property Number() As Integer
        Get
            Return Me.number_value
        End Get
        Set
            Me.number_value = value
        End Set
    End Property
End Class

Class Program

    Public Shared Sub Main()
        ' Create claim with custom claim type and primitive resource
        Dim c1 As New Claim("http://example.org/claims/simplecustomclaim", "Driver's License", Rights.PossessProperty)
        ' Create claim with custom claim type and structured resource type
        Dim c2 As New Claim("http://example.org/claims/complexcustomclaim", New MyResourceType("Martin", 38), Rights.PossessProperty)
    End Sub
End Class
' Do something with claims

関連項目