How to reset or show password using forgotten page?
In this article I am going to explain about in detail how to reset or show login password using forgotten page. This code snippet is help you whenever you need password reset option in your website.
Description :
In this aticle I have explained in detail about three way to show or rest login password
1) Directly send login password to registered email id.
2) Ask security question to show login password in webpage itself if security answer is correct
3) Send password reset link to the registered email id after user click that link to reset new login password.1) Directly send login password to registered email id.
In this option I have send login password directly to the registered email id.
I have design for that paced one text box and button control.
Enter User Id <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br/>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />Code Behind
//First Option send password to registered email id
protected void Button1_Click(object sender, EventArgs e)
{
try
{
sqlcon.Open();
//Retrieve password and email id from database using entered user id
sqlcmd = new SqlCommand("select * from reg where uname='" + TextBox1.Text.Trim() + "'", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
string toid = dt.Rows[0]["email"].ToString();
string msgsubject = "Forgotton password";
string mailContent = "Please find below your password details:
Password: " + dt.Rows[0]["pwd"] + "";
try
{
MailMessage msg = new MailMessage();
msg.To.Add(toid);
MailAddress frmAdd = new MailAddress(frmgmail);
msg.From = frmAdd;
msg.Subject = msgsubject;
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential NetCrd = new NetworkCredential(frmgmail, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
TextBox1.Text = "";
lblmsg.Text = "Password details send to registered email id please check!";
}
catch (Exception ex)
{
lblmsg.Text = "Unable to send Mail Please try again later";
}
}
else
{
lblmsg.Text = "User id not exist!";
}
}
catch (Exception ex)
{
}
finally
{
sqlcon.Close();
}
}
2) Ask security question to show login password in webpage itself.
In this option I have show password in the webpage itself after user give correct security answer
Enter User Id<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br/>
<asp:Button ID="Button2" runat="server" Text="Click here to answer security question" OnClick="Button2_Click" /> Code Behind
//Second Option security question and answer
protected void Button2_Click(object sender, EventArgs e)
{
Session["uname"] = TextBox2.Text.Trim();
Response.Redirect("SecurityQuestion.aspx");
}
In the security questions page load security question as per user selection at that time of registration
I have design in tat security page like this
Security question<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br/>
Enter answer <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
Security questions page code behind
Here I have validated user enter correct answer or not if answer is correct then show directly login password in this page.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class SecurityQuestion : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
string qn, ans,pwd;
protected void Page_Load(object sender, EventArgs e)
{
lblmsg.Text = "";
if (Session["uname"] == null)
{
Response.Redirect("Default.aspx");
}
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from reg where uname='" + Session["uname"].ToString() + "'", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
qn = dt.Rows[0]["secqn"].ToString();
ans = dt.Rows[0]["secans"].ToString();
pwd = dt.Rows[0]["pwd"].ToString();
if (!Page.IsPostBack)
{
DropDownList1.Items.Add(qn);
}
}
else
{
lblmsg.Text = "Invalid User Id";
TextBox1.Enabled = false;
}
}
catch (Exception ex)
{
}
finally
{
sqlcon.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == ans)
{
lblmsg.Text = "Your password is <b>" + pwd + "<br/>";
}
else
{
lblmsg.Text = "Your security answer is wrong !!! ";
}
}
}
3) Send password reset link to the registered email id.
In this option I have send login password reset link to registered email id if user is click that link in that reset page collect new login password and update it.
I have design for that paced one text box and button control.
Enter User Id<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <br/>
<asp:Button ID="Button3" runat="server" Text="Click here to send forgotton password recover link" onclick="Button3_Click" /> Code Behind
//Third Option - Reset password link
protected void Button3_Click(object sender, EventArgs e)
{
Class1 obj =new Class1();
string url;
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from reg where uname='" + TextBox3.Text.Trim() + "'", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
string toid = dt.Rows[0]["email"].ToString();
url = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf('/')) + "/" + "ResetPwd.aspx?uid=" + obj.EncryptUserId(TextBox3.Text.Trim());
string msgsubject = "Forgotton password";
string mailContent = "Please click below link to reset your password :
URL : " + url + "";
try
{
MailMessage msg = new MailMessage();
msg.To.Add(toid);
MailAddress frmAdd = new MailAddress(frmgmail);
msg.From = frmAdd;
msg.Subject = msgsubject;
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential NetCrd = new NetworkCredential(frmgmail, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
lblmsg.Text = "Password reset link send to your email id please check!";
sqlcmd = new SqlCommand("update reg set pwdlink='Y' where uname='" + TextBox3.Text.Trim() + "'", sqlcon);
sqlcmd.ExecuteNonQuery();
TextBox3.Text = "";
}
catch (Exception ex)
{
lblmsg.Text = "Unable to send Mail Please try again later";
}
}
else
{
lblmsg.Text = "User id not exist!";
}
}
catch (Exception ex)
{
}
finally
{
sqlcon.Close();
}
}
Further more on complete code details for all operation download the complete source from below and test it.Source code:
Client Side: ASP.NET
Code Behind: C#Conclusion
I hope this code snippet is help you to forgotten page.