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



我已将项目中的标识更新为版本2,在AccountController中的ForgotPassword操作中,我在以下行中接收No IUserTokenProvider is registered error

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

然后在此基础上实现IUserTokenProvider接口,在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;
 }

但同样的错误再次出现。然后我在ForgotPassword操作中初始化manager.UserTokenProvider,一切都很好。

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

我不知道问题出在哪里。

我找到了解决方案。每件事都是在identityconfig中真正实现的。问题出现在CCD_ 8中。我制作了一个新的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;
    }
}

这项工作现在很好。

相关内容

最新更新