我在使用asp.net基本身份的ForgotPassword方法时遇到问题。在单步执行代码时,即使我已经确认var user = await UserManager.FindByNameAsync(model.Email);表中存在用户的电子邮件地址,null行仍会返回aspnetusers。我不确定为什么Visual Studio不允许我进入FindByNameAsync方法?不知道这是怎么回事?

public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
            // Don't reveal that the user does not exist or is not confirmed
            return View("ForgotPasswordConfirmation");
        }

        var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
        var callbackUrl = Url.Action("ResetPassword", "Account",
        new { UserId = user.Id, code = code }, protocol: Request.Url.Scheme);
        await UserManager.SendEmailAsync(user.Id, "Reset Password",
        "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");
        return View("ForgotPasswordConfirmation");
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

最佳答案

您正在尝试通过电子邮件地址查找用户。

您应该使用UserManager.FindByEmailAsync

关于c# - ASP.NET 'FindByNameAsync'返回null吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39522210/

10-12 20:16