Badmiral Badmiral - 3 months ago 27
C# Question

Razor Token ResetPassword Not working

I have an MVC3 Razor web site. I am following the tutorial here:

http://www.thecodingguys.net/tutorials/asp/webpages-membership-forgot-password-and-reset-password

I have been able to generate a token and send the email successfully

Then the email has me going to a resetpassword view at a link like this:

~/Account/resetpassword?token=fujgFIo7k27c72-UTTJeGA2fujgFIo7k27c72-UTTJeGA2

Now, here is my HttpGet method

[AllowAnonymous]
[HttpGet]
public ActionResult resetpassword()
{
ResetPasswordModel model = new ResetPasswordModel()
{
Password = String.Empty,
ConfirmPassword = String.Empty,
Token = String.Empty
};
return View(model);
}


All it does is pass in an instance of the model

My view for resetpassword.cshtml

@model RazorARPP.Models.ResetPasswordModel
@{
var token = Request["token"];
Model.Token = token;
}



<form action="" method="post" enctype="multipart/form-data" id="MyForm">
@Html.ValidationSummary(true)
<fieldset>
<legend>Reset Password</legend>
@Html.HiddenFor(m => m.Token)
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessage("password")

<br/>

@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessage("confirmPassword")

<input type="submit"/>

</fieldset>
</form>


Now My HttpPost method

[AllowAnonymous]
[HttpPost]
public ActionResult resetpassword(ResetPasswordModel model)
{
if (model.Password == model.ConfirmPassword)
{
WebSecurity.ResetPassword(model.Token, model.Password);
}
return RedirectToAction("Login");
}


It executes fine but the resetpassword isn't working. I made sure that that line is running in the debugger. Any suggestions as to what I am doing wrong? Thanks

Answer

How are you generating the reset token? Are you using the correct username?

WebSecurity.GeneratePasswordResetToken(model.Email)

Do a test where you generate a token and immediately pass it to WebSecurity.ResetPassword. I'm guessing resetpassword is not getting the values you are expecting