Implement Reset Password in ASP.Net

SOURCE CODE

On Forgot Password Page

static string DBCS = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
protected void btnForgotPass_Click(object sender, EventArgs e)
{
	try
	{
		using (SqlConnection conn = new SqlConnection(DBCS))
		{
			using (SqlCommand cmd = new SqlCommand("select * from tblUsers where EmailAddress = @EmailAddress"))
			{
				cmd.Parameters.AddWithValue("@EmailAddress", exampleInputEmail.Text);
				cmd.Connection = conn;
				cmd.Connection.Open();
				using (SqlDataReader sqlRdr = cmd.ExecuteReader())
				{
					if (sqlRdr.HasRows)
					{
						while (sqlRdr.Read())
						{
							int userId = sqlRdr.GetInt32(0);
							string fullName = sqlRdr.GetString(1);
							string emailId = sqlRdr.GetString(2);
							var guid = Guid.NewGuid();

							using (SqlCommand cmd1 = new SqlCommand("insert into tblForgotPasswordRequest values(@UniqueId, @UserId, @DateRequest)"))
							{
								cmd1.Parameters.AddWithValue("@UniqueId", guid);
								cmd1.Parameters.AddWithValue("@UserId", userId);
								cmd1.Parameters.AddWithValue("@DateRequest", DateTime.Now);
								cmd1.Connection = conn;
								cmd1.ExecuteNonQuery();
								Response.Write("<script>alert('Password reset link has mailed to you');</script>");

								string emailSubject = "Reset Password";
								string emailBody = "Hi, " + fullName + "</h1>";
								emailBody += "<a href='http://localhost/ResetPassword.aspx?Guid=" + guid.ToString() + "'>Reset Password</a>";
								string msg = SendMail(emailSubject, emailBody, emailId);
							}
						}
					}
					else
					{
						Response.Write("<script>alert('This Email Address does not exists in our database.');</script>");
					}
				}
			}
		}
	}
	catch (Exception ex)
	{
		Response.Write("<script>alert('Some Error Occurred');</script>");
	}
}
public static string SendMail(string emailSubject, string emailBody, string toEmail)
{
	try
	{
		MailMessage PassRecMail = new MailMessage("", toEmail);
		PassRecMail.Body = emailBody;
		PassRecMail.IsBodyHtml = true;
		PassRecMail.Subject = emailSubject;
		PassRecMail.Priority = MailPriority.High;
		SmtpClient SMTP = new SmtpClient("smtp.gmail.com", 587);
		SMTP.DeliveryMethod = SmtpDeliveryMethod.Network;
		SMTP.UseDefaultCredentials = false;
		SMTP.UseDefaultCredentials = true;
		SMTP.Credentials = new NetworkCredential()
		{
			UserName = "",
			Password = ""
		};
		SMTP.EnableSsl = true;
		SMTP.Send(PassRecMail);
		return "Mail Send Successfully";
	}
	catch (Exception ex)
	{
		return "Some Error Occurred.";
	}
}

On Reset Password Page

private static int UserId;
private static Guid guid;
static string DBCS = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
	try
	{
		guid = new Guid(Request.QueryString["Guid"]);
		if (guid != null)
		{
			using (SqlConnection conn = new SqlConnection(DBCS))
			{
				using (SqlCommand cmd = new SqlCommand("select * from tblForgotPasswordRequest where UniqueId = @UniqueId"))
				{
					cmd.Parameters.AddWithValue("@UniqueId", guid);
					cmd.Connection = conn;
					cmd.Connection.Open();
					using (SqlDataReader sqlRdr = cmd.ExecuteReader())
					{
						if (sqlRdr.HasRows)
						{
							while (sqlRdr.Read())
							{
								UserId = sqlRdr.GetInt32(1);
							}
						}
						else
							Response.Redirect("~/Default.aspx", false);
					} 
				}
			}
		}
	}
	catch (Exception ex)
	{
		Response.Redirect("~/Default.aspx", false);
	}
}
protected void btnResetPass_Click(object sender, EventArgs e)
{
	try
	{
		if (inputPassword.Text == inputRepeatPassword.Text)
		{
			using (SqlConnection conn = new SqlConnection(DBCS))
			{
				using (SqlCommand cmd = new SqlCommand("update tblUsers set Password = @Password where UserId = @UserId"))
				{
					cmd.Parameters.AddWithValue("@Password", exampleInputPassword.Text);
					cmd.Parameters.AddWithValue("@UserId", UserId);
					cmd.Connection = conn;
					cmd.Connection.Open();
					cmd.ExecuteNonQuery();

					Response.Write("<script>alert('Password successfully updated.');</script>");

					using (SqlCommand cmd1 = new SqlCommand("delete from tblForgotPasswordRequest where UniqueID = @UniqueID"))
					{
						cmd1.Parameters.AddWithValue("@UniqueID", guid);
						cmd1.Connection = conn;
						cmd1.ExecuteNonQuery();
					}
				}
			}
		}
		else
		{
			Response.Write("<script>alert('Password does not match');</script>");
		}
	}
	catch (Exception ex)
	{
		Response.Write("<script>alert('Some Error Occurred');</script>");
	}
}

SOURCE VIDEO

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top