ResetPassword (WebSecurity Ref)

The ResetPassword method resets a user password. It takes two arguments: the token and the new password.

Example

Sponsored Links

C# Code

@{
    
string token = Request["token"];
string newpassword = Request["newpassword"];
string confirmpassword = Request["confirmpassword"];
 
string msg = "";
 
 
//REDIRECT USER IF TOKEN IS EMPTY
if (string.IsNullOrEmpty(token))
{
    Response.Redirect("~/");
}
 
//proccess data on post
if (IsPost)
{
    
    newpassword = Request["newpassword"];
    confirmpassword = Request["confirmpassword"];
 
 
    if (newpassword.IsEmpty())
    {
       ModelState.AddError("newpassword", "Please enter a new password");
    }
    
    //MAKE SURE NEW PASSWORD AND CONFIRM PASSWORD MATCH
    if (newpassword != confirmpassword)
    {
        ModelState.AddError("confirmpassword", "Passwords do not match");
    }
 
    if (ModelState.IsValid)
    {
        try
        {
            //CHANGE PASSWORD
            if (WebSecurity.ResetPassword(token, newpassword))
            {
                 msg = "Your password has been reset";
            }else
            {
                //WE HAVE A PROBLEM!
                ModelState.AddFormError("Sorry something went wrong there!");
            }
           
        }catch (Exception ex)
        {
            msg = ex.Message;
        }
    }
}
 
 
}

HTML Code

<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
 
        @msg
 
        @Html.ValidationSummary(true)
 
        <form method="post">
              <div>
             <label>New Password</label>
              <input type="password" name="newpassword"/>
               @Html.ValidationMessage("newpassword")
            </div>
 
              <div>
             <label>Confirm Password</label>
              <input type="password" name="confirmpassword"/>
               @Html.ValidationMessage("confirmpassword")
            </div>
 
                   <input type="submit"/>
        </form>
 
    </body>
</html>

See Also

GeneratePasswordResetToken