更新标识1到2后,没有IUserTokenProvider注册错误

时间:2015-12-21 10:19:55

标签: asp.net-mvc asp.net-identity

我已将项目中的身份更新为版本2,并在ForgotPassword中的AccountController操作中更新了此行中的No IUserTokenProvider is registered error

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

然后我实施基于thisIUserTokenProvider接口,并在IdentityConfig中使用:

 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;
 }

但同样的错误再次发生。 然后我在manager.UserTokenProvider动作中初始化ForgotPassword,并且每件事情都运行良好。

 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);
        }

我不知道问题是什么。 谁能帮我 ? 谢谢你提前。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。所有的东西都是在identityconfig中真正实现的。问题发生在AccountController。我创建了一个UserManager的新对象,并没有使用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; }

在新版本中:

public AccountController()
        {
        }
public AccountController(ApplicationUserManager userManager)
{
    UserManager = userManager;
}

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

这项工作现在很好。