title: A Forgot Password Functionality in ASP.NET MVC4
body: <p>I am implementing a forgot password functionality in ASP.NET MVC4 but can't successfully allow users to reset their password.</p>
  
  <p>My MVC application displays a RECOVER password link on the login page for users who have forgotten their passwords.The first part of the application works fine and a mail is sent to the user ...but the later part to allow users to change their password is not working .</p>
  
  <p>This is my forgot password controller action method :</p>
  
  <pre><code>[AllowAnonymous]
  public ActionResult ForgotPassword()
  {
      return View();
  }
  
  [HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public ActionResult ForgotPassword(string UserName)
  {
      //Check user existence
      var user = Membership.GetUser(UserName);
  
      if (user == null)
      {
          TempData["Message"] = "User Not exist.";
      }
      else
      {
          //Generate password token
          var token = WebSecurity.GeneratePasswordResetToken(UserName);
  
          //Create URL with above token
          var resetLink = "&lt;a href='" + Url.Action("ResetPassword", "Account", new { un = UserName, rt = token }, "http") + "'&gt;Reset Password&lt;/a&gt;";
  
          //Get user emailid
          UsersContext db = new UsersContext();
          var emailid = (from i in db.UserProfiles
                         where i.UserName == UserName
                         select i.EmailId).FirstOrDefault();
  
          //Send mail
          string subject = "Password Reset Token";
          string body = "&lt;b&gt;Please find the Password Reset Token&lt;/b&gt;&lt;br/&gt;" + resetLink;
  
          //Edit it
          try
          {
              SendEMail(emailid, subject, body);
              TempData["Message"] = "Mail Sent.";
          }
          catch (Exception ex)
          {
              TempData["Message"] = "Error occured while sending email." + ex.Message;
          }
  
      }
  
      return View();
  }
  </code></pre>
  
  <p>This is my forgot password view :</p>
  
  <pre><code>@using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      &lt;fieldset&gt;
          &lt;legend&gt;Forgot Password Form&lt;/legend&gt;
          &lt;ol&gt;
              &lt;li&gt;
                  @Html.Label("User Name", new { @for = "UserName" })
                  @Html.TextBox("UserName")
                  &lt;span style="color:red;"&gt;@TempData["Message"]&lt;/span&gt;
              &lt;/li&gt;
          &lt;/ol&gt;
          &lt;input type="submit" value="Recover" /&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>The forgot password view page allows user to enter their username.
  I get the username and email id and build URL and send mail to the user.
  This part runs successfully.
  Now user is redirected to my reset password page.
  Here I wish to allow user to enter a new password and update my database.
  How should my reset controller action and view page be?</p>
  
  <p>I couldn't even display the old password using the following controller action: 
  (Where Am I going wrong ?)</p>
  
  <pre><code>[AllowAnonymous]
  public ActionResult ResetPassword(string un, string rt)
  {
      UsersContext db = new UsersContext();
  
      //Get userid of received username
      var userid = (from i in db.UserProfiles
                    where i.UserName == un
                    select i.UserId).FirstOrDefault();
  
  
      var yrpassword = (from k in db.webpages_Memberships
                        where k.UserId == userid
                        select k.Password);
  
      TempData["Message"] = "This is your user name" + un; 
      TempData["Message"] = "This is your user-id" + userid;
      TempData["Message"] = "This is your user password" + yrpassword;
  
      return View();
  }
  </code></pre>
  
  <p>This is the current view page of reset password controller action :</p>
  
  <pre><code>@model MvcApplication1.Models.webpages_Membership
  
  @{
      ViewBag.Title = "Reset Password";
  }
  
  &lt;hgroup class="title"&gt;
      &lt;h1&gt;@ViewBag.Title.&lt;/h1&gt;
  &lt;/hgroup&gt;
  
  &lt;section id="loginForm"&gt;
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;Password Reset Form&lt;/legend&gt;
          &lt;ol&gt;
              &lt;li&gt;
                  @Html.LabelFor(m =&gt; m.Password)
                  @Html.TextBoxFor(m =&gt; m.Password)
                  @Html.ValidationMessageFor(m =&gt; m.Password)
              &lt;/li&gt;                           
          &lt;/ol&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/fieldset&gt;        
          &lt;ol&gt;
              &lt;li&gt;
                  @Html.ActionLink("Login", "Login") You can now login to your account.
              &lt;/li&gt;
          &lt;/ol&gt;
  }
  &lt;/section&gt;
  </code></pre>
  
  <p>I know it requires an update query but somehow I can't get it right.</p>