我已将项目中的身份更新为版本2,并在ForgotPassword
中的AccountController
操作中更新了此行中的No IUserTokenProvider is registered error
:
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
然后我实施基于this的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;
}
但同样的错误再次发生。
然后我在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);
}
我不知道问题是什么。 谁能帮我 ? 谢谢你提前。
答案 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;
}
}
这项工作现在很好。