2

我正在尝试在我的 mvc Web 应用程序中添加重置密码功能。

当我输入我的电子邮件地址并提交表单时,我收到了包含令牌等的电子邮件,然后当我点击链接时,我被重定向到重置密码页面,在那里我输入了我的电子邮件和新的密码,然后当我点击重置按钮时,我从 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();
    }

还有我的 ForgotPassword 方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)

    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByNameAsync(model.Email);
            if (user == null)
            {
                // Don't reveal that the user does not exist or is not confirmed
                return View("ForgotPasswordConfirmation");
            }

            string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
            var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

            var emailTemplateQuery = await _emailTemplateService.Query(x => x.Slug.ToLower() == "forgotpassword").SelectAsync();
            var emailTemplate = emailTemplateQuery.Single();

            dynamic email = new Postal.Email("Email");
            email.To = user.Email;
            email.From = CacheHelper.Settings.EmailAddress;
            email.Subject = emailTemplate.Subject;
            email.Body = emailTemplate.Body;
            email.CallbackUrl = callbackUrl;
            EmailHelper.SendEmail(email);

            return RedirectToAction("ForgotPasswordConfirmation", "Account");
        }

ForgotPassword.cshtml 文件:

            <div class="panel-body">
                @using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                {
                    @Html.AntiForgeryToken()
                    <h4>[[[Enter your email.]]]</h4>
                    <hr /> 
                     @Html.ValidationSummary("", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                        <div class="col-md-10">
                            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" class="btn btn-default" value="[[[Email Link]]]" />
                        </div>
                    </div>
                }
            </div>

ForgotPasswordConfirmation.cshtml 文件:

            <div class="panel-body">                    
                <div>
                    <p>
                        [[[Please check your email to reset your password.]]]
                    </p>
                </div>
            </div>

重置密码视图模型:

public class ResetPasswordViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "[[[Email]]]")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "[[[The {0} must be at least {2} characters long.]]]", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "[[[Password]]]")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "[[[Confirm password]]]")]
    [Compare("Password", ErrorMessage = "[[[The password and confirmation password do not match.]]]")]
    public string ConfirmPassword { get; set; }

    public string Code { get; set; }
}
4

1 回答 1

1

感谢您的帮助。我添加了以下代码,我解决了问题。

重置密码方法:

string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var result = await UserManager.ResetPasswordAsync(user.Id, code, model.Password);
于 2017-03-21T18:50:54.787 回答