Star
13653 Points
5480 Posts
Apr 04, 2014 12:01 AM|Ashim Chatterjee|LINK
sure ... :) .. i made some minor changes for better performance , hope you wont mind. :) try below:
LOGIN PAGE:
SqlConnection cn=new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\test.mdf;Integrated Security=True;User Instance=True"); cn.Open(); string name = txtname.Text; string password=txtPasswd.Text; SqlCommand cm =new SqlCommand("Select COUNT(*) FROM login WHERE name=@name and password=@password",cn); cm.Parameters.Add("@name", SqlDbType.VarChar).Value = name; cm.Parameters.Add("@password", SqlDbType.VarChar).Value = password; int count=(int) cm.ExecuteScalar(); cn.Close(); if (count > 0) { Session["test"] = name; Session["user"] = Convert.ToString(Guid.NewGuid()); Response.Redirect("welcome.aspx"); } else { Response.Write("login failed"); }
WELCOME PAGE:
protected void Page_Load(object sender, EventArgs e) { if (Session["user"] != null && Session["test"] != null) { // let them access the page. lblName.Text = "Welcome " +Session["test"].ToString(); } else { //clear the cache and redirect back to login page. Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); Session.Abandon(); Response.Redirect("login.aspx"); } }
WEB.CONFIG:
<configuration> <system.web> <sessionState timeout="1"></sessionState> </system.web> </configuration>
sessionstate SessionTimeOut asp.net
Star
13653 Points
5480 Posts
Re: session expire after some time and redirect to login page
Apr 04, 2014 12:01 AM|Ashim Chatterjee|LINK
sure ... :) .. i made some minor changes for better performance , hope you wont mind. :) try below:
LOGIN PAGE:
WELCOME PAGE:
WEB.CONFIG:
sessionstate SessionTimeOut asp.net