2016-05-04 21 views
1

Ich versuche, manuelles Reset-Passwort bei ForgetPassword Token klicken. Aber wenn ich dieses Token mit Benutzer validiere, gibt es immer false zurück. Bitte helfen Sie mir in diesem hier ist mein CodeValidieren von PasswordResetToken für ResetPassword

[AllowAnonymous] 
public async Task<ActionResult> ResetPassword() 
{ 
    var provider = new DpapiDataProtectionProvider("AppName"); 
    var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>()); 
    userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation")); 
    string userId = Request.QueryString["UserId"]; 
    string code = Request.QueryString["code"]; 
    var user = await UserManager.FindByIdAsync(userId); 
    //if (!(await UserManager.ConfirmEmailAsync(userId, code)).Succeeded) 
    ApplicationDbContext context = new ApplicationDbContext(); 
    UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context); 
    if (!await userManager.UserTokenProvider.ValidateAsync("EmailConfirmation", code, new UserManager<ApplicationUser>(store) , user)) 
    { 
     return RedirectToAction("Message", "Home", new { status = false, message = "Invalid token, please retry." }); 
    } 
    return View("ResetPassword", new ResetPasswordModel { UserId = userId, Token = code }); 
} 

Auch dies der Code ist, wie ich PasswordResetToken

var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("AppName"); 
UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation")); 
var user = await UserManager.FindByNameAsync(model.Email); 
if (user == null)//|| !(await UserManager.IsEmailConfirmedAsync(user.Id))) 
{ 
    // Don't reveal that the user does not exist or is not confirmed 
    return Json(new { status = false, message = "User does not exist" }); 
} 
var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); 

Bitte helfen Sie mir in diesem

Antwort

4

Vielleicht ein bisschen spät erzeugen, aber ich ist vor kurzem auf eine ähnliche Anforderung gestoßen.

Das einzige Problem, das ich sehe, ist

if (!await userManager.UserTokenProvider.ValidateAsync("EmailConfirmation", code, new UserManager<ApplicationUser>(store) , user)) 
{ 
    return RedirectToAction("Message", "Home", new { status = false, message = "Invalid token, please retry." }); 
} 

Ändern Sie diese zu

if (!await userManager.UserTokenProvider.ValidateAsync("ResetPassword", code, new UserManager<ApplicationUser>(store) , user)) 
{ 
    return RedirectToAction("Message", "Home", new { status = false, message = "Invalid token, please retry." }); 
} 

"EmailConfirmation" wird mit der Registrierung E-Mail verwendet werden, während "password" für die verwendet wird, ForgotPassword-Goodies.