gpt4 book ai didi

c# - 验证 ResetPassword 的 PasswordResetToken

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

我正在尝试在 ForgetPassword 标记点击时创建手动重置密码。但是当我用用户验证这个 token 时,它总是返回 false。请帮助我这是我的代码

[AllowAnonymous]
public async Task<ActionResult> ResetPassword()
{
var provider = new DpapiDataProtectionProvider("AppName");
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
string userId = Request.QueryString["UserId"];
string code = Request.QueryString["code"];
var user = await UserManager.FindByIdAsync(userId);
//if (!(await UserManager.ConfirmEmailAsync(userId, code)).Succeeded)
ApplicationDbContext context = new ApplicationDbContext();
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
if (!await userManager.UserTokenProvider.ValidateAsync("EmailConfirmation", code, new UserManager<ApplicationUser>(store) , user))
{
return RedirectToAction("Message", "Home", new { status = false, message = "Invalid token, please retry." });
}
return View("ResetPassword", new ResetPasswordModel { UserId = userId, Token = code });
}

这也是我生成 PasswordResetToken 的代码

var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("AppName");
UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null)//|| !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
// Don't reveal that the user does not exist or is not confirmed
return Json(new { status = false, message = "User does not exist" });
}
var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

请帮帮我

最佳答案

也许有点晚了,但我最近遇到了类似的需求。

我看到的唯一问题是

if (!await userManager.UserTokenProvider.ValidateAsync("EmailConfirmation", code, new UserManager<ApplicationUser>(store) , user))
{
return RedirectToAction("Message", "Home", new { status = false, message = "Invalid token, please retry." });
}

将此更改为

if (!await userManager.UserTokenProvider.ValidateAsync("ResetPassword", code, new UserManager<ApplicationUser>(store) , user))
{
return RedirectToAction("Message", "Home", new { status = false, message = "Invalid token, please retry." });
}

EmailConfirmation”将用于注册电子邮件,而“PasswordReset”用于忘记密码。

关于c# - 验证 ResetPassword 的 PasswordResetToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038135/

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