简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM