Introduction
Every registration system has a requirement but without forgot password, it's difficult to retrieve password. There is no other method through which you can access your forgotten password, so the main reason is to build a system through which users can access their forgotten passwords.
Background
Overall, it's necessary for a user if the passwords are complex and with a lot of websites in which you have an account in, you use multiple type of passwords (like add special characters or capital word including digits or any other way) according to the requirement of websites or apps. Without this, if you forget the password, you try and some websites have blockage of account after 5 to 10 tries depending on the scenario blocked for half an hour to 1 day but suppose you need it urgently, the way invented to make users relax is by giving a way to retrieve password through a link either with email or both security questions answer and email. That way, the user can easily retrieve the password in a minute or two.
Using the Code
In this tip, I am using only a simple way of retrieving password though email which you have used to register and later on for login.
First of all, add connection string in the web.config file and other app settings like key for toEmail
and SmtpServer
or other you would like to add define as a key in app settings and access in cs file.
<!---->
<connectionStrings>
<add name="ForgotPassword_Connstring" connectionString="" />
</connectionStrings>
<!---->
<appSettings>
<add key="toEmail" value=""/>
<add key="SmtpServer" value=""/>
</appSettings>
string connStringforgotpassword = System.Configuration.
ConfigurationManager.ConnectionStrings["ForgotPassword_Connstring"].ToString();
string fromEmail = ConfigurationManager.AppSettings
["toEmail"].ToString();
sObj.Host = ConfigurationManager.AppSettings
["SmtpServer"].ToString();
The code of RetreivePassword.aspx
page looks like this:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="RetreivePassword.aspx.cs"
Inherits="RetreivePassword" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Forgot Password</title>
<style type="text/css">
.style1
{
width: 500px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center" id="wrapper">
<br />
<br />
<h1>
FORGOT PASSWORD</h1>
<asp:Label ID="lblmessage"
runat="server"></asp:Label>
<asp:TextBox ID="txtforgot"
runat="server" Style="width: 300px;
height: 30px; color: White"></asp:TextBox>
<asp:Button ID="tbnforgot" runat="server"
class="login button" OnClick="tbnforgot_Click"
Text="Forgot Pssword" CausesValidation="true" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="txtforgot" Display="Dynamic"
ForeColor="Red" SetFocusOnError="true"
ErrorMessage="Please enter email"
ID="RequiredFieldValidator1"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtforgot" Display="Dynamic"
ForeColor="Red" SetFocusOnError="True"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
">Please enter valid email</asp:RegularExpressionValidator>
<br />
</div>
</form>
</body>
</html>
Reference
to be used in cs page is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;
using System.Drawing;
and for RetreivePassword.aspx.cs is:
public partial class RetreivePassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void tbnforgot_Click(object sender, EventArgs e)
{
try
{
DataSet dsResult = new DataSet();
string connStringforgotpassword = System.Configuration.
ConfigurationManager.ConnectionStrings["ForgotPassword_Connstring"].ToString();
using (SqlConnection con = new SqlConnection(connStringforgotpassword))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Email,
Password FROM Users Where Email= '" + txtforgot.Text.Trim() +
"' and IsActice = 1 and Isdeleted=0", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsResult);
con.Close();
}
if (dsResult.Tables[0].Rows.Count > 0)
{
string fromEmail = ConfigurationManager.AppSettings
["toEmail"].ToString();
MailMessage mmObj = new MailMessage();
mmObj.From = new MailAddress(fromEmail);
mmObj.To.Add(txtforgot.Text);
mmObj.IsBodyHtml = true;
mmObj.Subject = "Forgot Password";
mmObj.Body = "Hi,<br/><br />Welcome to
My Application <br/><br />Your UserName : " +
dsResult.Tables[0].Rows[0]["Email"].ToString() + "
and Password is : " + dsResult.Tables[0].Rows[0]
["Password"].ToString() + "";
mmObj.Priority = MailPriority.High;
SmtpClient sObj = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential
("your email (use mostly domain emails like ends
with .com/.net/.org e.t.c)", "your email password");
sObj.UseDefaultCredentials = false;
sObj.Credentials = myCredential;
sObj.Host = ConfigurationManager.AppSettings
["SmtpServer"].ToString(); sObj.Port = 25;
sObj.Send(mmObj);
lblmessage.Text = "Your Password Details Sent to '" + txtforgot.Text + "'";
lblmessage.ForeColor = Color.Aqua;
txtforgot.Text = "";
}
else
{
lblmessage.Text = "The Email you entered does not
exist in our database. Please Enter authentic email address";
lblmessage.ForeColor = Color.Orange;
}
}
catch (Exception ex)
{
throw ex;
}
}
}