title: Complex Claim Values in .NET Framework with System.Security.Claims
body: <p>I'm developing a web app with Asp.Net 5 MVC, Owin and Oauth2 bearer token as auth type.</p>
  
  <p>Following <a href="http://www.garycrawford.co.uk/complex-claim-values-with-wif/" rel="noreferrer">this guide</a> that adds a custom complex claim Json serialized to an instance of <code>Microsoft.IdentityModel.Claims.ClaimsIdentity</code> with success, I've tried to replicate the same example using the <a href="http://msdn.microsoft.com/en-us/library/system.security.claims.claimsidentity%28v=vs.110%29.aspx" rel="noreferrer">ClaimsIdentity</a> on the <code>System.Security.Claims</code> namespace.</p>
  
  <p>Unluckily, it seems that adding a <code>complexClaim</code> to the <code>ClaimsIdentity</code> instance, the derived class type information is lost, and the claim is stored as a <code>System.Security.Claims.Claim</code>.</p>
  
  <pre><code>var complexClaim = new ComplexClaim&lt;UKPassport&gt;(@"http://it.test/currentpassport", passport);
  var claims = new List&lt;Claim&gt;() { complexClaim };
  identity.AddClaims(claims);
  </code></pre>
  
  <p>When I  try to get back the claim from identity, casting it to to a <code>ComplexClaim&lt;UKPassport&gt;</code> Type results in a null value.</p>
  
  <pre><code>var passportClaim = identity.Claims.FirstOrDefault&lt;Claim&gt;(c=&gt;c.Type == @"http://it.test/currentpassport") as ComplexClaim&lt;UKPassport&gt;;
  </code></pre>
  
  <p>The same example works perfectly using <code>Microsoft.IdentityModel.Claims</code>.</p>
  
  <p>Any hints?</p>
  
  <p>Here is the complete ported code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Newtonsoft.Json;
  using System.Security.Claims;
  
  namespace ConsoleApplication1
  {
      class Program {
      private static ClaimsIdentity identity = new ClaimsIdentity();
  
      static void Main(string[] args)
      {
          var oldPassport = CreatePassport();
          identity.AddPassport(oldPassport);
  
          var britishCitizen = identity.IsBritishCitizen();
          var hasExpired = identity.IsCurrentPassportExpired();
          Console.WriteLine(hasExpired); 
          Console.ReadLine();
      }
  
      private static UKPassport CreatePassport()
      {
          var passport = new UKPassport(
              code: PassportCode.GBR, 
              number: 123456789,
              expiryDate: DateTime.Now);
  
          return passport;
      }
  }
  
      public static class ClaimsIdentityExtensions {
      public static void AddPassport(this ClaimsIdentity identity, UKPassport passport)
      {
          var complexClaim = new ComplexClaim&lt;UKPassport&gt;(@"http://it.test/currentpassport", passport);
  
          var claims = new List&lt;Claim&gt;() { complexClaim };
          identity.AddClaims(claims);
      }
  
      public static bool IsCurrentPassportExpired(this ClaimsIdentity identity)
      {
          var passport = GetPassport(identity, @"http://it.test/currentpassport");
          return DateTime.Now &gt; passport.ExpiryDate;
      }
  
      public static bool IsBritishCitizen(this ClaimsIdentity identity)
      {
          var passport = GetPassport(identity, @"http://it.test/currentpassport");
          return passport.Code == PassportCode.GBR;
      }
  
      private static UKPassport GetPassport(this ClaimsIdentity identity, string passportType)
      {
          var passportClaim = identity.Claims.FirstOrDefault&lt;Claim&gt;(c=&gt;c.Type == @"http://it.test/currentpassport") as ComplexClaim&lt;UKPassport&gt;;
          return passportClaim.Value;
      }
  }
  
      public enum PassportCode
      {
          GBR,
  
          GBD,
  
          GBO,
  
          GBS,
  
          GBP,
  
          GBN
      }
  
  
      public class ComplexClaim&lt;T&gt; : Claim where T : ClaimValue
      {
          public ComplexClaim(string claimType, T claimValue)
              : this(claimType, claimValue, string.Empty)
          {
          }
  
          public ComplexClaim(string claimType, T claimValue, string issuer)
              : this(claimType, claimValue, issuer, string.Empty)
          {
          }
  
          public ComplexClaim(string claimType, T claimValue, string issuer, string originalIssuer)
              : base(claimType, claimValue.ToString(), claimValue.ValueType(), issuer, originalIssuer)
          {
          }
  
          public new T Value
          {
              get
              {
                  return JsonConvert.DeserializeObject&lt;T&gt;(base.Value);
              }
          }
      }
  
      public class UKPassport : ClaimValue
      {
          public const string Name = "UKPassport";
  
          private readonly PassportCode code;
          private readonly int number;
          private readonly DateTime expiryDate;
  
          public UKPassport(PassportCode code, int number, DateTime expiryDate)
          {
              this.code = code;
              this.number = number;
              this.expiryDate = expiryDate;
          }
  
          public PassportCode Code { get { return this.code; } }
          public int Number { get { return this.number; } }
          public DateTime ExpiryDate { get { return this.expiryDate; } }
  
          public override string ValueType()
          {
              return @"http://it.test/currentpassport";
          }
      }    
  
  public abstract class ClaimValue {
      public abstract string ValueType();
  
      public override string ToString()
      {
          return JsonConvert.SerializeObject(this);
      }
  }
  }
  </code></pre>