代码之家  ›  专栏  ›  技术社区  ›  Muhammad Ahmed AbuTalib

在aspnet标识中忘记密码

  •  0
  • Muhammad Ahmed AbuTalib  · 技术社区  · 7 年前

    我一直在跟踪这个 article

    这篇文章似乎不完整。

    这是我创建的Web API,用于根据本文生成忘记密码链接。

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

    正如我们所看到的,resetpassword是URL操作,当在收件箱中单击链接时,应该调用该操作。然而,本文没有给出resetpassword API的方法定义。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Derviş Kayımbaşıoğlu    7 年前

    控制器中必须有ResetPassword方法作为操作方法。

      //
      // GET: /Account/ResetPassword
      [AllowAnonymous]
      public ActionResult ResetPassword(string code)
      {
         return code == null ? View("Error") : View();
      }
    
      //
      // POST: /Account/ResetPassword
      [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
      {
         if (!ModelState.IsValid)
         {
            return View(model);
         }
    
         var user = await UserManager.FindByNameAsync(model.Email);
         if (user == null)
         {
            // Don't reveal that the user does not exist
            return RedirectToAction("ResetPasswordConfirmation", "Account");
         }
         var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
         if (result.Succeeded)
         {
            return RedirectToAction("ResetPasswordConfirmation", "Account");
         }
         AddErrors(result);
         return View();
      }
    

    来源: Link