title: Using OWIN Identity v2 Claims to track custom properties in WebAPI2 app
body: <p>getting my head wrapped around the new Identity framework and am trying to figure out how best to handle custom user properties. I have tried extending the IdentityUser, which works to store the information, but so far is requiring an additional db call to get the property back out. I am looking at switching to using claims to store/retrieve this information.</p>
  
  <p>First, the specific prop I want to store/retrieve is not unique to an individual user (many to one). Consider grouping users together in a custom Group structure. I want to store the GroupId for use in other related entities.</p>
  
  <p>I am able to store the GroupId (currently using the ClaimTypes.NameIdentifier which I don't think it the correct usage for that type, but...). But, when I go to retrieve that value, the claim type isn't found in the claims collection. It's in the db, so I know it's there. I'm missing something.</p>
  
  <p>FWIW: Since it's WebAPI, I'm not using a traditional sign-in. I'm using token auth. </p>
  
  <p>When I create the user, I have something like:</p>
  
  <pre><code>public async Task&lt;IdentityResult&gt; CreateUserAsync(string email, string password, string groupId)
      {
          var userId = ObjectId.GenerateNewId(DateTime.UtcNow).ToString(); // yes, it's a NoSQL store
          var user = new ApplicationUser
          {
              Id = userId,
              UserName = email
          };
  
          var claim = new IdentityUserClaim { ClaimType = ClaimTypes.NameIdentifier, ClaimValue = groupId, UserId = userId, Id = ObjectId.GenerateNewId(DateTime.UtcNow).ToString() };
          user.Claims.Add(claim);
  
          var result = await _UserManager.CreateAsync(user, password);
          return result;
      }
  </code></pre>
  
  <p>That creates what looks to be an appropriate db entry.</p>
  
  <p>When I retrieve the value, I get null reference errors. Here's that code via an extension method:</p>
  
  <pre><code>public static string GetGroupId(this IIdentity identity)
      {
          var claimsIdentity = identity as ClaimsIdentity;
          return claimsIdentity == null ? "" : claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;
      }
  </code></pre>
  
  <p>The error hits when trying to get Value as the FindFirst is returning a null value.</p>
  
  <p>Any hints or better/best practices here would be appreciated! Honestly, I'd prefer to just store this on the <code>ApplicationUser : IdentityUser</code> object, but I can't find a simple way of retrieving that of User.Identity in my api controller context without an additional call to the db.</p>