Sunday, March 1, 2015

Create Session on Login page using C# and redirect it to next page also expire session

  1. Create 2 Web form pages named: Login , named: Secondpage
  2. Now create two text boxes and one Login button
  3. on Button click event of Login page add this code: 
  4.  if (TextBox1.Text == "abc" && TextBox2.Text == "abc")
                {
                    Session["login"] = TextBox1.Text;
                    Session["password"] = TextBox2.Text;
                    Response.Redirect("Secondpage.aspx");// redirect page to Secondpage.aspx
  5. Now on Secondpage Page_Load event add below code
  6. if (Session["Login"] != null && Session["password"]!=null)
                {
                    Response.Write(Session["login"].ToString());
                    Response.Write(Session["password"].ToString());
                }
                else
                {
                    Response.Write("Session has expired");
                }
  7. To expire session add below code into Web.config. Session will be expired after 1 minute
  8. <sessionState mode="InProc" cookieless="false" timeout="1" />

No comments:

Post a Comment