Create Login and Logout Using Session and forget password and remember password with Email in C# Asp.net

Create login table in MSSQL 

Create Table tblLogin
(
LoginId int identity(1,1) primary Key,
UserName varchar(50),
Age int,
Email varchar(50),
UserPassword varchar(50),
UserRePassword varchar(50),
Gender varchar(20)
)

 Copy Below code and paste inside body
Login Page
UserName
Paaword
Remember Me:
Forget Password?


<center>
        <div>
          <fieldset style="border:1.5px solid; border-radius:35px; text-align:left; padding-left:35px; height:130px; width:300px;">
              <legend style="text-align:center">Login Page</legend>
            <table>
                <tr>
                    <td>UserName</td>
                    <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Paaword</td>
                    <td><asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Remember Me: </td>
                    <td><asp:CheckBox ID="chkMe" runat="server" /></td>
                </tr>
                <tr>
                    <td><a href="#" onclick="window.open('forgetPassword.aspx','FP','width=500, height=50,top=300,left=500,fullscreen=no,resizable=0');">Forget Password?</a> </td>
                    <td><asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" /></td>
                </tr>
                <tr>
                    <td><asp:Label ID="lblMessage" runat="server"></asp:Label></td>
                </tr>
            </table>
          </fieldset>
        </div>
       </center>


protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["UserName"] != null && Request.Cookies["UserPassword"] != null)
                {
                    txtUserName.Text = Request.Cookies["UserName"].Value;
                    txtPassword.Attributes["Value"] = Request.Cookies["UserPassword"].Value;
                }
            }

            txtUserName.Focus();
            //this.Page.SetFocus(txtUserName);
            Page.Form.DefaultFocus = txtUserName.ClientID;
        }


        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string Connection = System.Configuration.ConfigurationManager.ConnectionStrings["strconfig"].ConnectionString;
            using (SqlConnection con = new SqlConnection(Connection))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandText = "Select * from tblLogin where UserName = @UserName and UserPassword= @UserPassword";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
                    cmd.Parameters.AddWithValue("@UserPassword", txtPassword.Text);
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    cmd.ExecuteNonQuery();
                    //Session["UserName"] = dt.Columns["UserName"];

                    if (dt.Rows.Count > 0)
                    {
                        if (chkMe.Checked)
                        {
                            Response.Cookies["UserName"].Value = txtUserName.Text;
                            Response.Cookies["UserPassword"].Value = txtPassword.Text;

                            Response.Cookies["UserName"].Expires = DateTime.Now.AddMinutes(1);
                            Response.Cookies["UserPassword"].Expires = DateTime.Now.AddMinutes(1);
                        }
                        else
                        {
                            Response.Cookies["UserName"].Expires = DateTime.Now.AddMinutes(-1);
                            Response.Cookies["UserPassword"].Expires = DateTime.Now.AddMinutes(-1);
                        }

                        lblMessage.Text = "User has been Login Successfully...!";
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                        Session["UserName"] = txtUserName.Text;
                        Response.Redirect("Welcome.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "UserName or Password is not valid";
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }

0 Comments