我已将项目中的标识更新为版本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;
}
}
这项工作现在很好。