Search code examples
asp.net-mvcasp.net-identity

No IUserTokenProvider is registered error after update identity 1 to 2


I have updated identity in my project to version 2 and in ForgotPassword action in AccountController i receive No IUserTokenProvider is registered error in this line:

 string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

then i implement IUserTokenProvider interface based on this and in IdentityConfig i use :

 public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
 {
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
     //other code 
     manager.UserTokenProvider = new MyUserTokenProvider<ApplicationUser>();        
     return manager;
 }

but the same error occured again. then i initialize manager.UserTokenProvider in ForgotPassword action and every thing worked fine.

 public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByEmailAsync(model.Email);
                if (user == null)
                {
                    return View("error message");
                }

                UserManager.UserTokenProvider = new MyUserTokenProvider<ApplicationUser>();

                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
                return RedirectToAction("ForgotPasswordConfirmation", "Account");
            }

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

I don't know what the problem is.


Solution

  • i found the solution. every thing was implemented truly in identityconfig. the problem was in AccountController. i had made an new object of UserManager and did not used of injected object of userManager :

    public AccountController() 
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
                        {
        _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                        }
    
        public AccountController(UserManager<ApplicationUser> userManager)
                    {
                        UserManager = userManager;
                    }
    
                    public UserManager<ApplicationUser> UserManager { get; private set; }
    

    in new version :

    public AccountController()
            {
            }
    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }
    
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
    

    this work fine now.