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.