重置密码功能生成无效令牌

本文关键字:无效 令牌 功能 密码 | 更新日期: 2023-09-27 18:08:41

我正试图在我的Web API中开发重置密码功能。Email的生成是在Rest Web Service(Web API)中执行的。一旦用户收到生成的带有链接的电子邮件,当他单击链接时,用户应该被重定向到驻留在MVC网站中的ResetPasswordPage(该网页不在Web API中,而是在由我的同事开发的网站中),驻留在另一个端口。当我点击重置密码页面时,它会打开重置密码网页。然后我点击提交按钮,我得到一个"无效令牌"消息。

Web API(REST Web Service)代码如下

string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
            code = HttpUtility.UrlEncode(code);
            string forgotPasswordHost = System.Configuration.ConfigurationManager.AppSettings["ForgotPasswordURL"];
            string forgotPasswordURL = Url.Route("URLApi", new {controller = "ResetPassword", userId = user.Id, code = code });
            try
            {
                //await UserManager.SendEmailAsync(user.Id, "Reset Password", $"{url}");
                await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href='"" + forgotPasswordHost + forgotPasswordURL + "'">here</a>");
            }
            catch (Exception e)
            {
                return new Status
                {
                    status = "Invalid Input"
                };
            }

点击URL,它会转到Website

下面的代码
[AllowAnonymous]
        public ActionResult ResetPassword(string code)
        {
            return code == null ? View("Error") : View();
        }

我从下面的方法(这是网站代码)获得无效令牌

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

点击电子邮件链接,网页打开。但我注意到,'_RequestVerificationToken'的值是不同的,当网页打开和页面提交后。如有任何帮助,不胜感激

重置密码功能生成无效令牌

找到答案了。我参考了以下链接:http://www.gunaatita.com/Blog/Invalid-Token-Error-on-Email-Confirmation-in-Aspnet-Identity/1056

这是由于Web API和MVC生成的机器密钥。在Web API和MVC上配置相同的机器密钥解决了"无效令牌"问题。