作法:建立自訂宣告

Windows Communication Foundation (WCF) 中的身分識別模型基礎結構會提供一組具有 Helper 函式的內建宣告類型與權限,可讓您透過這些類型與權限建立 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

另請參閱