I am following a bittech tutorial on creating identity cards and roles based on JWT. The user of my application is a user table user with int PK.
Currently, the GenerateUserIdentityAsync method just returns a strange error UserId not found . here is my code:
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
and implementation in the User object:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> manager, string authenticationType) {
My UserManager class is defined as follows:
public class AppUserManager : UserManager<User, int>
Oddly enough, when I debug, the this instance in GenerateIdentityAsync has the UserId property, but the base has only id , and I wonder if this is an error? (it doesn't sound right)
I looked at the source code (line 80), but I cannot figure out exactly where the exception is generated.
Exact exception:
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 the stack trace is not all that useful (to me)
How to find out why / where 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 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; } }