title: CreateUserIdenityAsync returns "UserId not found" exception for custom IdentityUser
body: <p>I'm following along the <a href="http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/" rel="nofollow">bitoftech tutorial</a> about creating Identity and role based claims with JWT. My application user is a custom User table with int PK. </p>
  
  <p>Currently, the GenerateUserIdentityAsync method just returns a weird <code>UserId not found</code> error. here's my code: </p>
  
  <pre><code>ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
  </code></pre>
  
  <p>and the implementation in <code>User</code> entity: </p>
  
  <pre><code>public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;User, int&gt; manager, string authenticationType)
  {
      //error on this line: CreateIdentityAsync throws error
      var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
      return userIdentity;
  }
  </code></pre>
  
  <p>My UserManager class is defined like so: </p>
  
  <pre><code>public class AppUserManager : UserManager&lt;User, int&gt;
  </code></pre>
  
  <p>Weirdly enough, when I debug, the instance <code>this</code> in <code>GenerateIdentityAsync</code> does have a <code>UserId</code> property, but the base only has an <code>id</code> and I wonder if that is where it's erroring out? (it doesnt sound right)</p>
  
  <p>I was looking at the <a href="https://www.symbolsource.org/MyGet/Metadata/aspnetwebstacknightly/Project/Microsoft.AspNet.Identity.Core/2.0.0-rtm-140226/Release/Default/Microsoft.AspNet.Identity.Core/Microsoft.AspNet.Identity.Core/ClaimsIdentityFactory.cs?ImageName=Microsoft.AspNet.Identity.Core" rel="nofollow">source code</a> (line 80) but I can't figure out where the exception is being thrown.</p>
  
  <p>The exact exception being thrown is: </p>
  
  <pre><code> UserId not found. 
   Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: 
      System.InvalidOperationException: 
      UserId not found.
  </code></pre>
  
  <p>And <a href="http://pastebin.com/kTdbeLVk" rel="nofollow">stack trace</a> isn't all that helpful (to me)</p>
  
  <p><strong>How do I find out why / where the UserId is not available?</strong></p>
  
  <hr>
  
  <p>Mode details: </p>
  
  <p>My <code>GrantResourceOwnerCredentials()</code>:</p>
  
  <pre><code>public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  {
      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] {"*"});
  
      var userManager = context.OwinContext.GetUserManager&lt;ApplicationUserManager&gt;();
      User user = await userManager.FindAsync(context.UserName, context.Password);
  
      if (user == null) // this is NOT null
      {
          context.SetError("invalid_grant", "The username or password is incorrect");
          return;
      }
  
      // this line fails
      ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT"); 
      var ticket = new AuthenticationTicket(oAuthIdentity, null);
      context.Validated(ticket);
  }
  </code></pre>
  
  <p>And the <code>ApplicationUser</code> (which, in my case, is just <code>User</code>)</p>
  
  <pre><code>public partial class User : IdentityUser&lt;int, CustomUserLogin, CustomUserRole, CustomUserClaim&gt;
  {  
      public int UserId { get; set; }
      public string Fullname { get; set; }
      public string Address { get; set; }
      public string ContactNumber { get; set; }
  }
  </code></pre>