0

i have a login page in asp.net..if successfully logged in ,it shows login time in next page.. then how to write a common function to detect session timeout and redirect into login.aspx page ?so that i can call it into all other pages

public partial class Login : System.Web.UI.Page
{
MainClass obj = new MainClass();

protected void bt_login_Click(object sender, EventArgs e)
{
    string s_name;

    SqlCommand cmd = new SqlCommand("select staff_id,staff_name from staff_details where staff_id='" + tb_loginid.Text + "' ", con);
    con.Open();

    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows == true)
    {
        if (tb_password.Text == "ABCD" )
        {
            dr.Read();

            string id = dr[0].ToString();    
            s_name = dr[1].ToString();
            Session["staffname"] = s_name;
            Session["staffid"] = tb_loginid;

            String last_interaction_time = DateTime.Now.ToShortTimeString();
            Session["lasttime"] = last_interaction_time;
            Response.Redirect("Successfully_loggedin.aspx");
        }
    }
        else
        {  ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('Incorrect LoginID or Password!')", true);
            lb_invalid.Visible = true;
            tb_password.Text = "";
        }
}

}

and logged_in page is

public partial class Successfully_logined : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

 string name=(string)Session["staffname"];
 lb_welcome.Text = "Welcome " + name+"!";

 string last_login_time= (string)Session["lasttime"];
 lb_logintime.Text =last_login_time;
}

}

and web.config is

<sessionState mode="InProc" cookieless="false" timeout="1">

nsds
  • 961
  • 4
  • 13
  • 39

1 Answers1

0

For that you have check condition..You check on Pageload also....

     if (Session["Username"] != null)
     { 
           // Code here 
     }
     else
     {
         Response.Redirect("login.aspx");
     }

For more details... Click here ... Session_timeout

Community
  • 1
  • 1
Niks
  • 997
  • 2
  • 10
  • 28