Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AspNetCore.Identity Reset Password-实例';X';无法跟踪_C#_Asp.net Core_Entity Framework Core_Invalidoperationexception_Asp.net Core Identity - Fatal编程技术网

C# AspNetCore.Identity Reset Password-实例';X';无法跟踪

C# AspNetCore.Identity Reset Password-实例';X';无法跟踪,c#,asp.net-core,entity-framework-core,invalidoperationexception,asp.net-core-identity,C#,Asp.net Core,Entity Framework Core,Invalidoperationexception,Asp.net Core Identity,我正在尝试使用AspNetCore.Identity程序集实现密码重置机制。它从一个用户说他们忘记了密码开始,输入他们的电子邮件和密码,然后提交。最后调用这个代码 IdentUser user = UserManager.FindByName(passwordObj.username); //Some unrelated code in between Task<string> codeTask = UserManager.GeneratePasswordResetTokenAs

我正在尝试使用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);
IdentUser user=UserManager.FindByName(passwordObj.username);
//中间有一些不相关的代码
Task codeTask=UserManager.GeneratePasswordResetTokenAsync(用户);
字符串代码=Base64ForUrlEncode(codeTask.Result);
字符串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;

....
}
public JsonResult ResetPassword(ResetPasswordModel passwordObj)
{
IdentUser User=UserManager.FindById(passwordObj.userId);
//无关代码
Task resetPassTask=UserManager.ResetPasswordAsync(用户,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
和异步调用似乎不太协调。请随意评论任何解释

可能与此无关,但您不等待异步调用的理由是什么?