Search code examples
c#asp.netasp.net-web-apiasp.net-identity

CreateUserIdenityAsync returns "UserId not found" exception for custom IdentityUser


I'm following along the bitoftech tutorial about creating Identity and role based claims with JWT. My application user is a custom User table with int PK.

Currently, the GenerateUserIdentityAsync method just returns a weird UserId not found error. here's my code:

ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");

and the implementation in User entity:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> manager, string authenticationType)
{
    //error on this line: CreateIdentityAsync throws error
    var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
    return userIdentity;
}

My UserManager class is defined like so:

public class AppUserManager : UserManager<User, int>

Weirdly enough, when I debug, the instance this in GenerateIdentityAsync does have a UserId property, but the base only has an id and I wonder if that is where it's erroring out? (it doesnt sound right)

I was looking at the source code (line 80) but I can't figure out where the exception is being thrown.

The exact exception being thrown is:

 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.

And stack trace isn't all that helpful (to me)

How do I find out why / where the UserId is not available?


Mode details:

My GrantResourceOwnerCredentials():

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] {"*"});

    var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
    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);
}

And the ApplicationUser (which, in my case, is just User)

public partial class User : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{  
    public int UserId { get; set; }
    public string Fullname { get; set; }
    public string Address { get; set; }
    public string ContactNumber { get; set; }
}

Solution

  • As you found out while debugging IdentityUser has an Id which in your case would represent the User's Id.

    You need to remove the UserId from your User class, use the base Id from IdentityUser and rename the UserId column in your custom User table to Id.

    Any properties you have in your User class needs to also have a matching column in your user table in the database. If not then you will get the same error for properties that do not match.

    That would mean Fullname, Address and ContactNumber must have matching column names in the AspNetUsers table or else you will get the same error for those properties as well.