Send Forgetten Password and Username in Email in ASP.Net

We have a login panel and account page and a forgetten password link, the user can login with his account details, but if he forgets the password and username he/she can access the username and password via the forgetten password link, in that they must feed their email and username and password will be sent to their respective emails.

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an empty website, provide a suitable name (LoginForm_demo).

Step 2

In Solution Explorer you get your empty website, then add three Web Forms and SQL Server database.

For Web Form

LoginForm_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it Login_demo.aspx. Again get to the same process and add another Web form and name it (Redirectpage.aspx). Again add one new web form and name it (ForgetPass.aspx).

For SQL Server Database

LoginForm_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add the database inside the App_Data_folder.)

Database Chamber

Step 3

In Server Explorer, click on your database (Database.mdf) then selet Tables -> Add New Table. Make the table like this:

Table -> tbl_data (Don't Forget to make ID as IS Identity -- True).

design view

Now enter some data into your database by going to Tables then right-click then select Show Table Data then enter whatever you want to add in username and password. I had entered the following data.

table design
We will match this username and password so if the data is correct the user is redirected to his account page or otherwise the user gets an error message.

Design code

Step 4

Now make some design for your application by going to 3 web forms and try the code like the following.

Login_demo.aspx

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>  
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <headrunat="server">  
  6.         <title></title>  
  7.         <styletype="text/css">  
  8. .style1  
  9. {  
  10. text-decoration: underline;  
  11. font-size: large;  
  12. text-align: left;  
  13. color: #0000FF;  
  14. }  
  15. .style2  
  16. {  
  17. width: 141px;  
  18. }   
  19.   
  20.   
  21.         </style>  
  22.     </head>  
  23.     <body>  
  24.         <formid="form1"runat="server">  
  25.             <div>  
  26.                 <tablestyle="width:100%;">  
  27.                     <captionclass="style1">  
  28.                         <strong>Login Form</strong>  
  29.                     </caption>  
  30.                     <tr>  
  31.                         <tdclass="style2">  
  32.    
  33.                         </td>  
  34.                         <td>  
  35.  </td>  
  36.                         <td>  
  37.  </td>  
  38.                     </tr>  
  39.                     <tr>  
  40.                         <tdclass="style2">  
  41. Username:  
  42.                         </td>  
  43.                         <td>  
  44.                             <asp:TextBoxID="TextBox1"runat="server">  
  45.                             </asp:TextBox>  
  46.                         </td>  
  47.                         <td><%--   
  48.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"   
  49. ControlToValidate="TextBox1" ErrorMessage="Please Enter Your Username"   
  50. ForeColor="Red"></asp:RequiredFieldValidator>--%>  
  51.                         </td>  
  52.                     </tr>  
  53.                     <tr>  
  54.                         <tdclass="style2">  
  55. Password:  
  56.                         </td>  
  57.                         <td>  
  58.                             <asp:TextBoxID="TextBox2"TextMode="Password"runat="server">  
  59.                             </asp:TextBox>  
  60.                         </td>  
  61.                         <td><%--   
  62.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"   
  63. ControlToValidate="TextBox2" ErrorMessage="Please Enter Your Password"   
  64. ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>--%>  
  65.                         </td>  
  66.                     </tr>  
  67.                     <tr>  
  68.                         <tdclass="style2">  
  69.    
  70.                         </td>  
  71.                         <td>  
  72.  </td>  
  73.                         <td>  
  74.  </td>  
  75.                     </tr>  
  76.                     <tr>  
  77.                         <tdclass="style2">  
  78.                             <asp:LinkButtonID="LinkButton1"runat="server"onclick="LinkButton1_Click">Forget Password??  
  79.                             </asp:LinkButton>  
  80.                         </td>  
  81.                         <td>  
  82.                             <asp:ButtonID="Button1"runat="server"Text="Log In"onclick="Button1_Click"/>  
  83.                         </td>  
  84.                         <td>  
  85.                             <asp:LabelID="Label1"runat="server">  
  86.                             </asp:Label>  
  87.                         </td>  
  88.                     </tr>  
  89.                 </table>  
  90.             </div>  
  91.         </form>  
  92.     </body>  
  93. </html>  
Redirectform.aspx
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Redirectform.aspx.cs"Inherits="Redirectform"%>  
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <headrunat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <formid="form1"runat="server">  
  10.             <div>  
  11.                 <p>  
  12.                     <strongstyle="font-size: xx-large">Hello Everyone! Welcome to my Page.  
  13.   
  14.   
  15.                     </strong>  
  16.                 </p>  
  17.             </div>  
  18.             <asp:ImageID="Image1"runat="server"Height="335px"  
  19. ImageUrl="~/redirectpageimage.jpg"Width="817px"/>  
  20.             <p>  
  21.  </p>  
  22.             <tablestyle="width:100%;">  
  23.                 <tr>  
  24.                     <td>  
  25.  </td>  
  26.                     <td>  
  27.  </td>  
  28.                     <td>  
  29.  </td>  
  30.                 </tr>  
  31.                 <tr>  
  32.                     <td>  
  33.  </td>  
  34.                     <td>  
  35.                         <asp:ButtonID="Button2"runat="server"onclick="Button2_Click"  
  36. style="margin-left: 313px"Text="Logout"/>  
  37.                     </td>  
  38.                     <td>  
  39.  </td>  
  40.                 </tr>  
  41.                 <tr>  
  42.                     <td>  
  43.  </td>  
  44.                     <td>  
  45.  </td>  
  46.                     <td>  
  47.  </td>  
  48.                 </tr>  
  49.             </table>  
  50.         </form>  
  51.     </body>  
  52. </html>  
Forgetpassword.aspx
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="ForgetPassword.aspx.cs"Inherits="Default2"%>  
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <headrunat="server">  
  6.         <title></title>  
  7.         <styletype="text/css">  
  8. .style1  
  9. {  
  10. width: 225px;  
  11. }  
  12. .style2  
  13. {  
  14. width: 155px;  
  15. }  
  16. .style6  
  17. {  
  18. text-align: left;  
  19. font-size: large;  
  20. text-decoration: underline;  
  21. color: #0000FF;  
  22. }  
  23.   
  24.         </style>  
  25.     </head>  
  26.     <body>  
  27.         <formid="form1"runat="server">  
  28.             <div>  
  29.                 <tablestyle="width:100%;">  
  30.                     <captionclass="style6">  
  31.                         <strong>ForgetPassword:</strong>  
  32.                     </caption>  
  33.                     <tr>  
  34.                         <tdclass="style1">  
  35.    
  36.                         </td>  
  37.                         <tdclass="style2">  
  38.    
  39.                         </td>  
  40.                         <td>  
  41.  </td>  
  42.                         <td>  
  43.  </td>  
  44.                     </tr>  
  45.                     <tr>  
  46.                         <tdclass="style1">  
  47.    
  48.                         </td>  
  49.                         <tdclass="style2">  
  50. EmailId:  
  51.                         </td>  
  52.                         <td>  
  53.                             <asp:TextBoxID="TextBox1"runat="server">  
  54.                             </asp:TextBox>  
  55.                         </td>  
  56.                         <td>  
  57.  </td>  
  58.                     </tr>  
  59.                     <tr>  
  60.                         <tdclass="style1">  
  61.    
  62.                         </td>  
  63.                         <tdclass="style2">  
  64.    
  65.                         </td>  
  66.                         <td>  
  67.  </td>  
  68.                         <td>  
  69.  </td>  
  70.                     </tr>  
  71.                     <tr>  
  72.                         <tdclass="style1">  
  73.                             <asp:LabelID="lbmsg"runat="server">  
  74.                             </asp:Label>  
  75.                         </td>  
  76.                         <tdclass="style2">  
  77.                             <asp:LinkButtonID="LinkButton1"runat="server"onclick="LinkButton1_Click">Back to Login  
  78.                             </asp:LinkButton>  
  79.                         </td>  
  80.                         <td>  
  81.                             <asp:ButtonID="Button1"runat="server"onclick="Button1_Click"Text="Submit"/>  
  82.                         </td>  
  83.                         <td>  
  84.  </td>  
  85.                     </tr>  
  86.                 </table>  
  87.             </div>  
  88.         </form>  
  89.     </body>  
  90. </html>  
Your design will look like the following.

Login.aspx

design login form

ForgetPassword.aspx

design forgetpassword

Code Chamber

Step 5

We will make some code in the respective web forms.cs file, starting from login.aspx.cs.

Login.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9.   
  10. publicpartialclass_Default: System.Web.UI.Page {  
  11.     protectedvoid Page_Load(object sender, EventArgs e) {  
  12.   
  13.     }  
  14.     protectedvoid Button1_Click(object sender, EventArgs e) {  
  15.   
  16.   
  17.         SqlConnection con = newSqlConnection(@  
  18.         "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  19.         SqlCommand cmd = newSqlCommand("select * from tbl_data where username=@username and password=@password", con);  
  20.         cmd.Parameters.AddWithValue("@username", TextBox1.Text);  
  21.         cmd.Parameters.AddWithValue("password", TextBox2.Text);  
  22.         SqlDataAdapter sda = newSqlDataAdapter(cmd);  
  23.         DataTable dt = newDataTable();  
  24.         sda.Fill(dt);  
  25.         con.Open();  
  26.         int i = cmd.ExecuteNonQuery();  
  27.         con.Close();  
  28.   
  29.         if (dt.Rows.Count > 0) {  
  30.             Response.Redirect("Redirectform.aspx");  
  31.         } else {  
  32.             Label1.Text = "You're username and password is incorrect";  
  33.             Label1.ForeColor = System.Drawing.Color.Red;  
  34.   
  35.         }  
  36.   
  37.   
  38.     }  
  39.     protectedvoid LinkButton1_Click(object sender, EventArgs e) {  
  40.   
  41.         Response.Redirect("ForgetPassword.aspx");  
  42.     }  
  43. }  
ForgetPassword.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Net;  
  10. using System.Net.Mail;  
  11.   
  12.   
  13. publicpartialclassDefault2: System.Web.UI.Page {  
  14.     protectedvoid Page_Load(object sender, EventArgs e) {  
  15.   
  16.     }  
  17.     protectedvoid Button1_Click(object sender, EventArgs e) {  
  18.         string username = "";  
  19.         string password = "";  
  20.         SqlConnection con = newSqlConnection(@  
  21.         "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  22.         SqlCommand cmd = newSqlCommand("select username, password from tbl_data where email=@email", con);  
  23.         cmd.Parameters.AddWithValue("email", TextBox1.Text);  
  24.         con.Open();  
  25.         using(SqlDataReader sdr = cmd.ExecuteReader()) {  
  26.   
  27.             if (sdr.Read()) {  
  28.                 username = sdr["username"].ToString();  
  29.                 password = sdr["password"].ToString();  
  30.   
  31.             }  
  32.   
  33.         }  
  34.         con.Close();  
  35.   
  36.         if (!string.IsNullOrEmpty(password)) {  
  37.             MailMessage msg = newMailMessage();  
  38.             msg.From = newMailAddress("[email protected]");  
  39.             msg.To.Add(TextBox1.Text);  
  40.             msg.Subject = " Recover your Password";  
  41.             msg.Body = ("Your Username is:" + username + "<br/><br/>" + "Your Password is:" + password);  
  42.             msg.IsBodyHtml = true;  
  43.   
  44.             SmtpClient smt = newSmtpClient();  
  45.             smt.Host = "smtp.gmail.com";  
  46.             System.Net.NetworkCredential ntwd = newNetworkCredential();  
  47.             ntwd.UserName = "[email protected]"//Your Email ID  
  48.             ntwd.Password = ""// Your Password  
  49.             smt.UseDefaultCredentials = true;  
  50.             smt.Credentials = ntwd;  
  51.             smt.Port = 587;  
  52.             smt.EnableSsl = true;  
  53.             smt.Send(msg);  
  54.             lbmsg.Text = "Username and Password Sent Successfully";  
  55.             lbmsg.ForeColor = System.Drawing.Color.ForestGreen;  
  56.         }  
  57.     }  
  58.   
  59.   
  60.   
  61.     protectedvoid LinkButton1_Click(object sender, EventArgs e) {  
  62.         Response.Redirect("Default.aspx");  
  63.     }  
  64. }  
Redirectform.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. publicpartialclassRedirectform: System.Web.UI.Page {  
  9.     protectedvoid Page_Load(object sender, EventArgs e) {  
  10.   
  11.     }  
  12.     protectedvoid Button2_Click(object sender, EventArgs e) {  
  13.         Response.Redirect("Default.aspx");  
  14.     }  
  15. }  
Output Chamber

login form

mypage

I haven't used an actual logout, it's just redirected to the login page. You can make a logout on your own using session. I had done it in my previous articles, just it check out in case you need it.

forget password

recover your password

I hope you like this. Thank you for reading. Have a good day.

 


Similar Articles