How to do both sliding and absolute timeout in asp.net forms authentication

醉酒当歌 提交于 2019-12-05 18:02:36

You can start a new session with the current time when the user's session begins in the Global.asax file, then with every subsequent request, compare the session's value with the current time until it is >= to current time.

void Application_AcquireRequestState(object sender, EventArgs e)
{
    if (HttpContext.Current.Session != null)
    {
        DateTime started = (DateTime)HttpContext.Current.Session["SessionStarted"];
        DateTime current = DateTime.Now;
        double totalHours = started.Subtract(current).TotalHours;
        if (totalHours >= 24)
        {
            //end session
        }
    }
}

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    Session["SessionStarted"] = DateTime.Now;
}

HttpApplication.AcquireRequestState Event

Occurs when ASP.NET acquires the current state (for example, session state) that is associated with the current request.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!