If someone else makes the same mistake as me. I tried to make a function similar to the one below and I'm pretty sure that the error is “No IUserTokenProvider”. gone. However, as soon as I tried to use the link created from "callbackUrl", I got the error "Invalid token." For it to work, you need to get the HttpContext UserManager. If you are using the standard ASP.NET MVC 5 application with separate user accounts, you can do this as shown below.
Code that works:
public ActionResult Index() { //Code to create ResetPassword URL var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = userManager.FindByName("useremail@gmail.com"); string code = userManager.GeneratePasswordResetToken(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); return View(); }
First try what doesn't work, No IUserTokenProvider is registered. disappeared, but the generated URL gets an Invalid token. .
public ActionResult NotWorkingCode() { //DOES NOT WORK - When used the error "Invalid token." will always trigger. var provider = new DpapiDataProtectionProvider("ApplicationName"); var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext())); userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("ASP.NET Identity")); var user = userManager.FindByName("useremail@gmail.com"); string code = userManager.GeneratePasswordResetToken(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //DOES NOT WORK - When used the error "Invalid token." will always trigger. return View(); }
Ogglas Dec 29 '15 at 16:59 2015-12-29 16:59
source share