0

In my login page, User enters his username and password. If credentials are valid, then I "save" this user in Session variable.

On every page, In Page_Load event, I am checking his rights. If session of saved user is null, then application redirects him on the login page. In most cases user is not null, but sometimes the session is lost during use of application.

My login.aspx.cs:

protected void btn_logIn_Click(object sender, EventArgs e)
{
      string username =  txt_user.Text;
      string pass= txt_pass.Text;
      if (checkUser(username ,pass)==true)
      {
          User loggedIn=new User();
          loggedIn=GetUser(username);
          Session["user"]=loggedIn;
          Response.Redirect("page.aspx",false);
      }
 }

protected void Page_Load(object sender, EventArgs e)
{
      User loggedIn=(User)Session["user"];
      if (loggedIn=!null)
      {
           Response.Redirect("Index.aspx",false);
      }

 }

Every other .aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
      User loggedIn=(User)Session["user"];
      if (loggedIn==null)
      {
           Response.Redirect("login.aspx",false);
      }
      else 
      {
           //Checking his rights...
      }
 }

Web.config

<sessionState mode="InProc" timeout="20"></sessionState>

BTW On localhost it works perfect. After i publish app, this happens.

EDIT: With further testing, I see strange results. For example, when my user is returned to login page, becase "he is not" in session, he can get back to Entry page (see login.aspx.cs, why is that), after few times of pressing F5 (refresh page).

What causes this problem? Looks like session data is still here, but not always, or what?

Thank you.

tadej
  • 701
  • 1
  • 5
  • 22

1 Answers1

1

Because you have set

sessionState mode="InProc" timeout="20"

in the Web.config which means that Session will be useless after 20 miniutes.

This is a good method to enhance the safety of you web sites.

Liam
  • 27,717
  • 28
  • 128
  • 190
William H
  • 11
  • 2