![](/img/trans.png)
[英]identity server 4 + AspNetCore.Identity: how to lockout after X tries?
[英]AspNetCore.Identity Reset Password - Instance of 'X' cannot be tracked
我正在尝试使用AspNetCore.Identity程序集实现密码重置机制。 首先,用户说他们忘记了密码,然后输入电子邮件和密码,然后提交。 最终调用此代码
IdentUser user = UserManager.FindByName(passwordObj.username);
//Some unrelated code in between
Task<string> codeTask = UserManager.GeneratePasswordResetTokenAsync(user);
string code = Base64ForUrlEncode(codeTask.Result);
string applicationUrl = string.Format("{0}/{1}?userId={2}&&token={3}", ApplicationUrl, "ResetPasswordView", user.Id, code);
生成重置密码令牌没有任何问题,任务运行完成,并且收到了包含正确URL的电子邮件。
但是,当我尝试重置密码时,我得到了这段代码
public JsonResult ResetPassword(ResetPasswordModel passwordObj)
{
IdentUser User = UserManager.FindById(passwordObj.userId);
//Unrelated code
Task<IdentityResult> resetPassTask = UserManager.ResetPasswordAsync(User, Base64ForUrlDecode(passwordObj.token), passwordObj.resetPassword);
IdentityResult user = resetPassTask.Result;
....
}
并且resetPassTask.Result
行将生成一个异常,指出“ 无法跟踪实体类型'IdentUser'的实例,因为已经跟踪了具有相同键的该类型的另一个实例 。
我对ASP.NET Core还是比较陌生,并且刚刚开始学习异步调用的来龙去脉,因此我在调试此代码时遇到了困难。 我一直在寻找答案,但是没有找到解决我问题的方法,所以我在这里。 关于如何解决或调试此问题的任何想法?
因此,我想出了我的问题,并且正在发布我的修复程序,以便希望对面临此问题的其他人有所帮助。
我的FindById函数是在AspNetCore.Identity的UserManager IdentUserManager
的扩展中实现的。 FindById不是异步的->
public IdentUser FindById(int userId)
{
return _dbContext.Users.Include(user => user.IdentUserProfile)
.Include(role => role.IdentUserProfile.IdentUserOrgRole)
.Include(client => client.IdentUserProfile.IdentMapApplicationsWithUser).FirstOrDefault(x => x.Id == userId);
}
因此,我现在使用的是AspNetCore.Identity的FindByIdAsync
IdentUser User = await UserManager.FindByIdAsync(passwordObj.userId.ToString());
这解决了我的问题。 不能完全确定原因是什么,但是直接使用DbContexts
和异步调用似乎不太好。 随意发表任何解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.