title: How do I implement a password Reset Link
body: <p>I currently have a system where if a user has forgotten their password, they can reset it by clicking on a forgot password link. They will be taken to a page where they enter in their username/email and then an email will be sent to the user, I wanted to know how can I implement a password reset link in the email so once the user clicks on the link he/she is taken to a page which will allow them to reset their password.</p>
  
  <p>This is the code in my controller </p>
  
  <pre><code>public ActionResult ForgotPassword()
          {
             //verify user id
  
              string UserId = Request.Params ["txtUserName"];
              string msg = "";
              if (UserId == null) 
              {
                  msg = "You Have Entered An Invalid UserId - Try Again";
                  ViewData["ForgotPassword"] = msg;
                  return View("ForgotPassword");
              }
  
              SqlConnection lsql = null;
              lsql = DBFactory.GetInstance().getMyConnection();
  
              String sqlstring = "SELECT * from dbo.[USERS] where USERID = '" + UserId.ToString() + "'";
              SqlCommand myCommand = new SqlCommand(sqlstring, lsql);
              lsql.Open();
              Boolean validUser;         
              using (SqlDataReader myReader = myCommand.ExecuteReader())
              {
  
                  validUser = false;
                  while (myReader.Read())
                  {
                      validUser = true;
  
                  }
                  myReader.Close();
              }
              myCommand.Dispose();  
  
  
              if (!validUser) 
                    {
                  msg = "You Have Entered An Invalid UserId - Try Again";
                  ViewData["ForgotPassword"] = msg;
                  lsql.Close();
                  return View("ForgotPassword");
              }
  
              //run store procedure
  
  
              using (lsql)
              {
                  SqlCommand cmd = new SqlCommand("Stock_Check_Test.dbo.RESET_PASSWORD", lsql);
                  cmd.CommandType = CommandType.StoredProcedure;
  
                  SqlParameter paramUsername = new SqlParameter("@var1", UserId);
  
                  cmd.Parameters.Add(paramUsername);
  
  
                  SqlDataReader rdr = cmd.ExecuteReader();
                  while (rdr.Read())
                  {
                      if (Convert.ToInt32(rdr["RC"]) == 99)
                      {
                          msg = "Unable to update password at this time";
                          ViewData["ForgotPassword"] = msg;
                          lsql.Close();
                          return View("ForgotPassword");  
  
                      }
                  }
              }
  
  
              msg = "new password sent";
              ViewData["ForgotPassword"] = msg;
              lsql.Close();
              return View("ForgotPassword");
          }
  </code></pre>
  
  <p>This is my current stored procedure which sends the user an email</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[A_SEND_MAIL]
      @var1 varchar (200), -- userid
      @var2 varchar (200) -- email address
  AS
  BEGIN
  declare @bodytext varchar(200);
  set @bodytext = 'Password Reset for user: ' +@var1 + ' @' + cast (getDate() as varchar) + ' ' ;
  EXEC msdb.dbo.sp_send_dbmail 
  @profile_name='Test',
  @recipients=@var2,
  @subject='Password Reset',
  @body=@bodytext
  END 
  
  GO
  </code></pre>