Identity 2.1 - UserId not found but was working before

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 19:20:42
edtruant

Having the same error right now! The only relevant recent action I've made is the update of packages through NuGet:

  • EntityFramework 6.1.1 to 6.1.3
  • Microsoft ASP.NET IdentityCore 2.1.0to 2.2.1
  • Microsoft ASP.NET Identity EntityFramework 2.1.0 to 2.2.1
  • Microsoft ASP.NET Identity Owin 2.1.0 to 2.2.1

Still investigating anyway...

UPDATE

In my case I was trying to generate the confirmation token before saving the user. I learned that UserManager.GenerateEmailConfirmationTokenAsync(userID) generates the token upon a saved ID, not the one you have in memory.

Hope this helps, somehow.

This exception is not generated by Application User class, there is no property named UserID in this class. (its Id not UserId)

So there are two Tables in Identity database contains column with UserID

  1. IdentityRole
  2. IdentityUserRole

Therefore exception is being raised during insert in these tables, there is nothing wrong with ApplicationUser.Id. Make sure that role with name "User" exists. also check for name case i.e. "User"/"user"

If, like me, you struggle to get identity framework working without fully appreciating all the ins and outs, this may help. I was using this line to get a reference to the user:

var user = new ApplicationUser { UserName = ...

This creates a new user. What I should have done is use this line:

var user = UserManager.FindByEmail( ...

You can then reset the password like this:

string code = UserManager.GeneratePasswordResetToken(user.Id);
var result = UserManager.ResetPassword(user.Id, code, model.Password);

Note that this has no error-checking.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!