ASP.NET Core错误:实施忘记密码时出现无效令牌错误
在VS2017 ver 15.3.3
具有Individual User Accounts
模式的ASP.NET MVC Core 1.1.1
应用程序中,我正在实现Forgot Password
function。 该应用正确地使用生成的链接发送电子邮件。 我可以打开我的电子邮件,可以看到生成的链接如下。 链接正确显示ResetPassWord.cshtml
视图。 但是当我输入所需的用户名,密码,确认密码信息并单击“提交”按钮时,我收到Invalid Token
错误。 问题 :如何解决问题?
注意 :
答:如果我已登录并希望将密码重置为其他内容,则PasswordReset
function可以正常工作。
B.不确定它是否相关:我没有使用电子邮件作为用户名。 相反,我改变了AccountCountroller
的Email属性
[Required] [EmailAddress] public string Email { get; set; }
至
[Required] public string UserName { get; set; }
等等,并在ApplicationUser.cs
扩展了用户属性,如下所示:
ApplicationUser.cs
// Add profile data for application users by adding properties to the ApplicationUser class public class ApplicationUser : IdentityUser { public string UserFullName { get; set; } [Column(TypeName = "varchar(50)")] public string UserEmaill { get; set; } [Column(TypeName = "varchar(15)")] public string UserCity{ get; set; } }
应用程序发送的电子邮件正文 :
Please reset your password by clicking here: link
控制器 :
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await _userManager.FindByNameAsync(model.UserName); string sUserEmail = user.UserEmail; //I Added this. Note the email is not the user name in my app if (user == null) { // Don't reveal that the user does not exist or is not confirmed return View("ForgotPasswordConfirmation"); } // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link var code = await _userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); await _emailSender.SendEmailAsync(sUserEmail, "Reset Password", $"Please reset your password by clicking here: link"); return View("ForgotPasswordConfirmation"); } // If we got this far, something failed, redisplay form return View(model); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { return View(model); } var user = await _userManager.FindByNameAsync(model.UserName); if (user == null) { // Don't reveal that the user does not exist return RedirectToAction(nameof(AccountController.ResetPasswordConfirmation), "Account"); } var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password); if (result.Succeeded) { return RedirectToAction(nameof(AccountController.ResetPasswordConfirmation), "Account"); } AddErrors(result); return View(); }
UPDATE
上述就是C#学习教程:ASP.NET Core错误:实施忘记密码时出现无效令牌错误分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
- 我试过
code = System.Net.WebUtility.UrlEncode(code);
行var code = await _userManager.GeneratePasswordResetTokenAsync(user);
在ForgotPassword(...)
POST方法中; 但仍然是同样的错误。 然后我也尝试了var code = System.Net.WebUtility.UrlDecode(model.Code);
在行var result = await _userManager.ResetPasswordAsync(user, code, model.Password);
在ResetPassword(...)
POST方法内; 同样的错误。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/957315.html