2016-09-16 63 views
1

我對基準asp.net身份的ForgotPassword方法有問題。在單步執行代碼時,行var user = await UserManager.FindByNameAsync(model.Email);返回null,即使我已確認該用戶的電子郵件地址存在於aspnetusers表中。我不確定爲什麼Visual Studio不會允許我進入FindByNameAsync方法?不知道這裏發生了什麼?ASP.NET'FindByNameAsync'返回null?

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); 
} 
+0

(https://msdn.microsoft.com/en-us/library/dn613101(V = vs.108)的.aspx),該方法採用一個用戶名。我不確定您的用戶名是否與電子郵件地址相同,但通常情況並非如此。也許嘗試從FindByNameAsync期望的用戶標識或使用FIndbyEmailAsync的模型中嘗試其他字段? –

+0

你必須使用這個方法var user = await UserManager.FindByEmailAsync(model.Email);因爲你的參數是電子郵件。 –

+0

我不能相信我錯過了那個。感謝您指點我正確的方向。 – PixelPaul

回答