user249375
user249375

Reputation:

set session timeout after inactivity

I want to redirect back to the home page when there is no activity. However, i dont have a user login or the need to have one. However, i want to be able to redirect back to the home page when theres no activity.

I set this in the web.conf

<system.web>
<sessionState mode="InProc"  timeout="2">

then, i set this in the homepage

Session["UserId"] = 1;

I also tried this but the function doesnt even fire.

protected void Page_Init(object sender, EventArgs e)
    {
        CheckSession();
    }


    private void CheckSession()
    {
        if (Session["UserId"] == null)
        {
            Response.Redirect("KioskHome.aspx");
        }
    }

Could i use the global.asax file?

void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {


    }

What is the simple solution? Thank you

Upvotes: 1

Views: 2333

Answers (1)

user111013
user111013

Reputation:

If I understand your question, you want to redirect the user's browser if the user hasn't performed any action in some period of time.

In that case - server-side behaviour won't help you. The connection has already closed. The global.asax Session_end will fire when the session is ending, and there won't be a client connected at that time. Perhaps you should read more about the ASP.NET Page Lifecycle.

What you may want, however, is some form of client-side behavior such as Javascript, which after a specific timeout, can redirect the user. Note that there's a number of issues with this, including that a user may use multiple tabs, so knowing accurately when the session has timed out is difficult.

Upvotes: 1

Related Questions