[英]Microsoft.AspNetCore.Identity.UserManager: Warning: VerifyUserTokenAsync() failed with purpose: ResetPassword for user
I'm using a custom IdentityUserInherit class that inherits IdentityUser class.我正在使用继承 IdentityUser class 的自定义 IdentityUserInherit class。 But, I haven't overridden the SecurityStamp property.
但是,我没有覆盖 SecurityStamp 属性。 The only overridden property is Id.
唯一被覆盖的属性是 Id。 When I try to reset password, it says invalid token.
当我尝试重置密码时,它显示无效令牌。 Just for the sake of the purpose of testing, I generated the token and immediately tryied to resetpassword but it say Invalid Token Below is my code:
只是为了测试的目的,我生成了令牌并立即尝试重置密码,但它说 Invalid Token 下面是我的代码:
[HttpPost]
public async Task<IActionResult> ForgotPassword(AdminForgotPassword adminForgot)
{
if (ModelState.IsValid)
{
var result = await _userManager.FindByEmailAsync(adminForgot.Email);
if (result != null)
{
var token = await _userManager.GeneratePasswordResetTokenAsync(new IdentityUserInherit
{
Id = result.Id
});
var resetresult = await _userManager.ResetPasswordAsync(new IdentityUserInherit
{
Email = adminForgot.Email
}, token, "Wizard9116");
Console.WriteLine(resetresult);
}
}
return View();
}
You need change your code like below:您需要更改您的代码,如下所示:
public class AccountController : Controller
{
//be sure T in UserManager<T> is the custom IdentityUserInherit class...
private readonly UserManager<AdminForgotPassword> _userManager;
public AccountController(UserManager<AdminForgotPassword> userManager)
{
_userManager = userManager;
}
[HttpPost]
public async Task<IActionResult> ForgotPassword(AdminForgotPassword adminForgot)
{
if (ModelState.IsValid)
{
var result = await _userManager.FindByEmailAsync(adminForgot.Email);
if (result != null)
{
//change here...
var token = await _userManager.GeneratePasswordResetTokenAsync(result);
var resetresult = await _userManager.ResetPasswordAsync(result, token, "Wizard9116");
Console.WriteLine(resetresult);
}
}
return View();
}
}
Testing Model:测试 Model:
public class AdminForgotPassword : IdentityUser
{
}
DbContext:数据库上下文:
public class ApplicationDbContext : IdentityDbContext<AdminForgotPassword>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.