我在那个令牌上花了好几个小时的绝望。我可以创建它,它写在电子邮件链接和查询字符串中 - 但它不会传输到表单的隐藏字段,因此不会到达控制器。
//send email
var callbackUrl = Url.Action(controller: "Account", action: "ResetPassword", values: new { userId = user.Id, code = code }, protocol: Request.Scheme);
await this._emailSender.SendEmailAsync(
email: user.Email,
username: user.UserName,
subject: "Reset Password",
message: callbackUrl);
Resetpassword.cshtml
@model ViewModel.ResetPasswordViewModel
@{
ViewData["Title"] = "Reset password";
}
<h2>@ViewData["Title"].</h2>
<form asp-controller="Account" asp-action="ResetPassword" method="post" class="form-horizontal" role="form">
<h4>Reset your password.</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<input asp-for="Code" type="hidden" />
<div class="form-group">
<label asp-for="Email" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Password" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Reset" />
</div>
</div>
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
代码在那里 - 但 仍为空。
所以我的模型在Code中包含null,在_RequestVerificationToken
中包含null这是我的ResetPasswordViewModel:
public class ResetPasswordViewModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.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; }
public string _RequestVerificationToken { get; set; }
}
我尝试了不同的例子并且没有改变它们,但令牌对我来说不起作用。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
{
var user = await _userManager.FindByEmailAsync(model.Email);
var identityResult = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);
//var identityResult = await _userManager.ResetPasswordAsync(user, model._RequestVerificationToken, model.Password);
if (!identityResult.Succeeded)
{
return View("Error");
}
return RedirectToAction("Index", "Home"); //ToDo new View
//var user = await _userManager.FindByEmailAsync(model.Email);
//if (user == null)
// throw new InvalidOperationException();
//if (model.Password != model.ConfirmPassword)
//{
// ModelState.AddModelError(string.Empty, "Passwords do not match");
// return View();
//}
//var resetPasswordResult = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);
//if (!resetPasswordResult.Succeeded)
//{
// foreach (var error in resetPasswordResult.Errors)
// ModelState.AddModelError(string.Empty, error.Description);
// return View();
//}
//return Content("Password updated");
}
有什么想法吗?