简体   繁体   English

Microsoft.AspNetCore.Identity.UserManager:警告:VerifyUserTokenAsync() 失败,目的是:ResetPassword for user

[英]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.

相关问题 Microsoft.AspNetCore.Identity.UserManager:警告:用户验证失败:InvalidUserName;InvalidEmail - Microsoft.AspNetCore.Identity.UserManager:Warning: User validation failed: InvalidUserName;InvalidEmail 没有注册 'Microsoft.AspNetCore.Identity.UserManager`1[System.Model.User]' 类型的服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[System.Model.User]' has been registered 无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager InvalidOperationException:无法解析类型为“Microsoft.AspNetCore.Identity.UserManager”的服务 - InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` AspNetCore.Identity.UserManager[9] VerifyUserTokenAsync() "code": "InvalidToken", - AspNetCore.Identity.UserManager[9] VerifyUserTokenAsync() "code": "InvalidToken", 没有注册“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”类型的服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered 尝试激活“WebShop.Controllers.User.UserController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'WebShop.Controllers.User.UserController' 没有为“Microsoft.AspNetCore.Identity.UserManager`1[testLogin.Areas.Identity.Data.testLoginUser]”类型注册服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[testLogin.Areas.Identity.Data.testLoginUser]' has been registered InvalidOperationException:无法解析范围服务&#39;Microsoft.AspNetCore.Identity.UserManager .NET Core 2.0 - InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager .NET Core 2.0 ASP.Net Core 2.1:没有针对Microsoft.AspNetCore.Identity.UserManager类型的服务 - ASP.Net Core 2.1: No service for type Microsoft.AspNetCore.Identity.UserManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM