[英]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.