【问题标题】:Forgot password in aspnet identity忘记了 aspnet 身份中的密码
【发布时间】:2019-01-21 04:38:21
【问题描述】:

我一直在关注这个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) 创建一个新的 ASP.NET 项目并选择身份验证 > 本地用户。步骤 2) 将您想要的代码复制到您的项目中。完成
  • 卡米洛可能确实有帮助。但是,互联网上没有资源可以谈论这个嗯?
  • 不确定,但代码是自动生成的代码的复制粘贴。另外,如果你刚开始,你应该从 ASP.NET Core 开始,ASP.NET MVC 5 不会持续很长时间

标签: c# asp.net-web-api asp.net-identity


【解决方案1】:

控制器内部必须有 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

【讨论】:

  • 这看起来很有希望。我会尽快实现这个想法
猜你喜欢
  • 2016-12-06
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 2020-05-10
  • 2019-10-01
  • 2022-07-23
相关资源
最近更新 更多