如何:创建自定义声明

Windows Communication Foundation (WCF) 中的标识模型基础结构提供了一组内置的声明类型和权限,并且提供了用来创建具有这些类型和权限的 Claim 实例的帮助器函数。这些内置的声明用于对 WCF 默认支持的客户端凭据类型中的信息进行建模。在许多情况下,这些内置的声明足够满足需要,然而一些应用程序可能需要自定义声明。声明由声明类型、要应用该声明的资源和在该资源上断言的权限组成。本主题描述如何创建自定义声明。

创建基于基元数据类型的自定义声明

  1. 通过将声明类型、资源值和权限传递到 Claim 构造函数来创建自定义声明。

    1. 确定声明类型的唯一值。

      声明类型是一个唯一的字符串标识符。自定义声明的设计者负责确保声明类型所使用的字符串标识符是唯一的。有关 WCF 定义的声明类型的列表,请参见 ClaimTypes 类。

    2. 选择资源的基元数据类型和值。

      资源是一个对象。资源的 CLR 类型可以是一个基元类型,例如 StringInt32,也可以是任何可序列化的类型。资源的 CLR 类型必须是可序列化的,因为 WCF 会在不同时刻将声明序列化。基元类型是可序列化的类型。

    3. 选择 WCF 定义的一个权限,或者为自定义权限选择一个唯一值。

      权限是一个唯一的字符串标识符。WCF 定义的权限是在 Rights 类中定义的。

      自定义声明的设计者负责确保权限所使用的字符串标识符是唯一的。

      下面的代码示例创建了一个声明类型为 http://example.org/claims/simplecustomclaim、资源名称为 Driver's License 并具有 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)
    
    // Create claim with custom claim type and primitive resource
    Claim c1 = new Claim ( "http://example.org/claims/simplecustomclaim", "Driver's License", Rights.PossessProperty);
    

创建基于非基元数据类型的自定义声明

  1. 通过将声明类型、资源值和权限传递到 Claim 构造函数来创建自定义声明。

    1. 确定声明类型的唯一值。

      声明类型是一个唯一的字符串标识符。自定义声明的设计者负责确保声明类型所使用的字符串标识符是唯一的。有关 WCF 定义的声明类型的列表,请参见 ClaimTypes 类。

    2. 为资源选择或定义一个可序列化的非基元类型。

      资源是一个对象。资源的 CLR 类型必须是可序列化的,因为 WCF 会在不同时刻将声明序列化。基元类型本身是可序列化的类型。

      在定义一个新类型时,请将 DataContractAttribute 应用于类。另外,请将 DataMemberAttribute 属性应用于新类型的需要作为声明的一部分序列化的所有成员。

      下面的代码示例定义了一个名为 MyResourceType 的自定义资源类型。

      <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 'New
      
      
          Public Sub New(ByVal text As String, ByVal number As Integer) 
              Me.text_value = text
              Me.number = number
      
          End Sub 'New
      
          ' 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
      
      [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; } }
      }  
      
    3. 选择 WCF 定义的一个权限,或者为自定义权限选择一个唯一值。

      权限是一个唯一的字符串标识符。WCF 定义的权限是在 Rights 类中定义的。

      自定义声明的设计者负责确保权限所使用的字符串标识符是唯一的。

      下面的代码示例创建了一个声明类型为 http://example.org/claims/complexcustomclaim、自定义资源类型为 MyResourceType 并具有 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)
    
    
    // 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);
    

示例

下面的代码示例演示如何创建一个具有基元资源类型的自定义声明和一个具有非基元资源类型的自定义声明。

Imports System
Imports System.IdentityModel.Claims
Imports System.Runtime.Serialization
Imports System.Security.Permissions


<assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution := True)>

<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 'New
    
    
    Public Sub New(ByVal text As String, ByVal number As Integer) 
        Me.text_value = text
        Me.number = number
    
    End Sub 'New
    
    ' 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 Class 'MyResourceType

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)
    
' Do something with claims
using System;
using System.IdentityModel.Claims;
using System.Runtime.Serialization;
using System.Security.Permissions;
[assembly: SecurityPermission(
   SecurityAction.RequestMinimum, Execution = true)]
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
    }
  }
}

另请参见

参考

Claim
Rights
ClaimTypes
DataContractAttribute
DataMemberAttribute

概念

使用标识模型管理声明和授权
使用标识模型管理声明和授权