gpt4 book ai didi

c# - 密码重置时 ASP.Net Identity “Invalid token”,密码中带 *

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:12 24 4
gpt4 key购买 nike

当用户在输入新密码后尝试在重置密码屏幕上重置密码时,我们会收到无效 token 错误消息。通常这对每个人都适用,即使有一些特殊字符,如#。我们现在遇到这样一种情况,有人在重置密码屏幕上的新密码中输入 *,只是因为这个特殊字符而收到此错误消息。

我现在已经尝试了数小时的研究来找到解决这种情况的原因,但没有成功。我找到了 this solution here用户名中的特殊字符有问题,但我们没有这个问题。密码中只有那个特殊字符有问题。由于我们已经在生产中,我们不能只是禁止密码中的该字符。

有人知道吗?

生成 token Controller 方法:

[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Email.ToLower());
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user.UserName)))
{
// 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 http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
var code = await _userManager.GeneratePasswordResetTokenAsync(user.UserName);
code = HttpUtility.UrlEncode(code);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.UserName, code = code }, protocol: Request.Url.Scheme);

await _emailService.CreateResetPasswordEmailAsync(user, callbackUrl);
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}

// If we got this far, something failed, redisplay form
return View(model);
}

重置密码 Controller 方法:

[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}

var user = await _userManager.FindByNameAsync(model.Email.ToLower());
if (user == null)
{
// Don't reveal that the user does not exist
return RedirectToAction("ResetPasswordConfirmation", "Account");
}

var result = await _userManager.ResetPasswordAsync(user.UserName, HttpUtility.UrlDecode(model.Code), model.Password);
if (result.Succeeded)
{
return RedirectToAction("ResetPasswordConfirmation", "Account");
}

AddErrors(result);
return View();
}

最佳答案

问题是您对重置 token 进行了双重编码。这里:

var code = await _userManager.GeneratePasswordResetTokenAsync(user.UserName);
code = HttpUtility.UrlEncode(code); //<--problem is this line
var callbackUrl = Url.Action("ResetPassword", "Account",
new { userId = user.UserName, code = code }, protocol: Request.Url.Scheme);

您对 token 进行编码,然后 Url.Action 将再次执行此操作。所以解决方案是不手动编码,让 MVC 为您处理 - 只需删除此处的第二行。

此外,在另一端,现在无需再次解码,因此您的代码将是:

var result = await _userManager.ResetPasswordAsync(user.UserName, 
model.Code, model.Password);

关于c# - 密码重置时 ASP.Net Identity “Invalid token”,密码中带 *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43411091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com