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).
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.
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
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
- <!DOCTYPEhtmlPUBLIC"-//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">
- <headrunat="server">
- <title></title>
- <styletype="text/css">
- .style1
- {
- text-decoration: underline;
- font-size: large;
- text-align: left;
- color: #0000FF;
- }
- .style2
- {
- width: 141px;
- }
-
-
- </style>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- <tablestyle="width:100%;">
- <captionclass="style1">
- <strong>Login Form</strong>
- </caption>
- <tr>
- <tdclass="style2">
-
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <tdclass="style2">
- Username:
- </td>
- <td>
- <asp:TextBoxID="TextBox1"runat="server">
- </asp:TextBox>
- </td>
- <td><%--
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
- ControlToValidate="TextBox1" ErrorMessage="Please Enter Your Username"
- ForeColor="Red"></asp:RequiredFieldValidator>--%>
- </td>
- </tr>
- <tr>
- <tdclass="style2">
- Password:
- </td>
- <td>
- <asp:TextBoxID="TextBox2"TextMode="Password"runat="server">
- </asp:TextBox>
- </td>
- <td><%--
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
- ControlToValidate="TextBox2" ErrorMessage="Please Enter Your Password"
- ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>--%>
- </td>
- </tr>
- <tr>
- <tdclass="style2">
-
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <tdclass="style2">
- <asp:LinkButtonID="LinkButton1"runat="server"onclick="LinkButton1_Click">Forget Password??
- </asp:LinkButton>
- </td>
- <td>
- <asp:ButtonID="Button1"runat="server"Text="Log In"onclick="Button1_Click"/>
- </td>
- <td>
- <asp:LabelID="Label1"runat="server">
- </asp:Label>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Redirectform.aspx
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Redirectform.aspx.cs"Inherits="Redirectform"%>
- <!DOCTYPEhtmlPUBLIC"-//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">
- <headrunat="server">
- <title></title>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- <p>
- <strongstyle="font-size: xx-large">Hello Everyone! Welcome to my Page.
-
-
- </strong>
- </p>
- </div>
- <asp:ImageID="Image1"runat="server"Height="335px"
- ImageUrl="~/redirectpageimage.jpg"Width="817px"/>
- <p>
- </p>
- <tablestyle="width:100%;">
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <asp:ButtonID="Button2"runat="server"onclick="Button2_Click"
- style="margin-left: 313px"Text="Logout"/>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Forgetpassword.aspx
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="ForgetPassword.aspx.cs"Inherits="Default2"%>
- <!DOCTYPEhtmlPUBLIC"-//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">
- <headrunat="server">
- <title></title>
- <styletype="text/css">
- .style1
- {
- width: 225px;
- }
- .style2
- {
- width: 155px;
- }
- .style6
- {
- text-align: left;
- font-size: large;
- text-decoration: underline;
- color: #0000FF;
- }
-
- </style>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- <tablestyle="width:100%;">
- <captionclass="style6">
- <strong>ForgetPassword:</strong>
- </caption>
- <tr>
- <tdclass="style1">
-
- </td>
- <tdclass="style2">
-
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <tdclass="style1">
-
- </td>
- <tdclass="style2">
- EmailId:
- </td>
- <td>
- <asp:TextBoxID="TextBox1"runat="server">
- </asp:TextBox>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <tdclass="style1">
-
- </td>
- <tdclass="style2">
-
- </td>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <tdclass="style1">
- <asp:LabelID="lbmsg"runat="server">
- </asp:Label>
- </td>
- <tdclass="style2">
- <asp:LinkButtonID="LinkButton1"runat="server"onclick="LinkButton1_Click">Back to Login
- </asp:LinkButton>
- </td>
- <td>
- <asp:ButtonID="Button1"runat="server"onclick="Button1_Click"Text="Submit"/>
- </td>
- <td>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Your design will look like the following.
Login.aspxForgetPassword.aspxCode ChamberStep 5
We will make some code in the respective web forms.cs file, starting from login.aspx.cs.
Login.aspx.cs
- 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;
-
- publicpartialclass_Default: System.Web.UI.Page {
- protectedvoid Page_Load(object sender, EventArgs e) {
-
- }
- protectedvoid Button1_Click(object sender, EventArgs e) {
-
-
- SqlConnection con = newSqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = newSqlCommand("select * from tbl_data where username=@username and password=@password", con);
- cmd.Parameters.AddWithValue("@username", TextBox1.Text);
- cmd.Parameters.AddWithValue("password", TextBox2.Text);
- SqlDataAdapter sda = newSqlDataAdapter(cmd);
- DataTable dt = newDataTable();
- sda.Fill(dt);
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
-
- if (dt.Rows.Count > 0) {
- Response.Redirect("Redirectform.aspx");
- } else {
- Label1.Text = "You're username and password is incorrect";
- Label1.ForeColor = System.Drawing.Color.Red;
-
- }
-
-
- }
- protectedvoid LinkButton1_Click(object sender, EventArgs e) {
-
- Response.Redirect("ForgetPassword.aspx");
- }
- }
ForgetPassword.aspx.cs
- 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.Net;
- using System.Net.Mail;
-
-
- publicpartialclassDefault2: System.Web.UI.Page {
- protectedvoid Page_Load(object sender, EventArgs e) {
-
- }
- protectedvoid Button1_Click(object sender, EventArgs e) {
- string username = "";
- string password = "";
- SqlConnection con = newSqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = newSqlCommand("select username, password from tbl_data where email=@email", con);
- cmd.Parameters.AddWithValue("email", TextBox1.Text);
- con.Open();
- using(SqlDataReader sdr = cmd.ExecuteReader()) {
-
- if (sdr.Read()) {
- username = sdr["username"].ToString();
- password = sdr["password"].ToString();
-
- }
-
- }
- con.Close();
-
- if (!string.IsNullOrEmpty(password)) {
- MailMessage msg = newMailMessage();
- msg.From = newMailAddress("[email protected]");
- msg.To.Add(TextBox1.Text);
- msg.Subject = " Recover your Password";
- msg.Body = ("Your Username is:" + username + "<br/><br/>" + "Your Password is:" + password);
- msg.IsBodyHtml = true;
-
- SmtpClient smt = newSmtpClient();
- smt.Host = "smtp.gmail.com";
- System.Net.NetworkCredential ntwd = newNetworkCredential();
- ntwd.UserName = "[email protected]";
- ntwd.Password = "";
- smt.UseDefaultCredentials = true;
- smt.Credentials = ntwd;
- smt.Port = 587;
- smt.EnableSsl = true;
- smt.Send(msg);
- lbmsg.Text = "Username and Password Sent Successfully";
- lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
- }
- }
-
-
-
- protectedvoid LinkButton1_Click(object sender, EventArgs e) {
- Response.Redirect("Default.aspx");
- }
- }
Redirectform.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- publicpartialclassRedirectform: System.Web.UI.Page {
- protectedvoid Page_Load(object sender, EventArgs e) {
-
- }
- protectedvoid Button2_Click(object sender, EventArgs e) {
- Response.Redirect("Default.aspx");
- }
- }
Output ChamberI 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.
I hope you like this. Thank you for reading. Have a good day.