Edit

Share via


How to: Create a Custom Claim

The Identity Model infrastructure in Windows Communication Foundation (WCF) provides a set of built-in claim types and rights with the helper functions for creating Claim instances with those types and rights. These built-in claims are designed to model information found in client credential types that WCF supports by default. In many cases, the built-in claims are sufficient; however some applications may require custom claims. A claim consists of the claim type, the resource for which the claim applies to and the right that is asserted over that resource. This topic describes how to create a custom claim.

To create a custom claim that is based on a primitive data type

  1. Create a custom claim by passing the claim type, resource value and right to the Claim(String, Object, String) constructor.

    1. Decide on a unique value for the claim type.

      The claim type is a unique string identifier. It is the custom claim designer's responsibility to ensure that the string identifier that is used for the claim type is unique. For a list of claim types that are defined by WCF, see the ClaimTypes class.

    2. Choose the primitive data type and value for the resource.

      A resource is an object. The CLR type of the resource can be a primitive, such as String or Int32, or any serializable type. The CLR type of the resource must be serializable, because claims are serialized at various points by WCF. Primitive types are serializable.

    3. Choose a right that is defined by WCF or a unique value for a custom right.

      A right is a unique string identifier. The rights that are defined by WCF are defined in the Rights class.

      It is the custom claim designer's responsibility to ensure that the string identifier that is used for the right is unique.

      The following code example creates a custom claim with a claim type of http://example.org/claims/simplecustomclaim, for a resource named Driver's License, and with the PossessProperty right.

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

To create a custom claim that is based on a non-primitive data type

  1. Create a custom claim by passing the claim type, resource value and right to the Claim(String, Object, String) constructor.

    1. Decide on a unique value for the claim type.

      The claim type is a unique string identifier. It is the custom claim designer's responsibility to ensure that the string identifier that is used for the claim type is unique. For a list of claim types that are defined by WCF, see the ClaimTypes class.

    2. Choose or define a serializable non-primitive type for the resource.

      A resource is an object. The CLR type of the resource must be serializable, because claims are serialized at various points by WCF. Primitive types are already serializable.

      When a new type is defined, apply the DataContractAttribute to the class. Also apply the DataMemberAttribute attribute to the all members of the new type that need to be serialized as part of the claim.

      The following code example defines a custom resource type named 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; } }
      }
      
    3. Choose a right that is defined by WCF or a unique value for a custom right.

      A right is a unique string identifier. The rights that are defined by WCF are defined in the Rights class.

      It is the custom claim designer's responsibility to ensure that the string identifier that is used for the right is unique.

      The following code example creates a custom claim with a claim type of http://example.org/claims/complexcustomclaim, a custom resource type of MyResourceType, and with the PossessProperty right.

      // 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);
      

Example

The following code example demonstrates how to create a custom claim with a primitive resource type and a custom claim with a non-primitive resource type.

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
    }
  }
}

See also