void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
DateTime? userLastActive = (DateTime?)HttpContext.Current.Session["UserLastActive"];
if (userLastActive.HasValue && DateTime.Now.Subtract(userLastActive.Value).Minutes > 15)
{
Session.Abandon();
return;
}
// Check if the request for background tasks
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
var action = HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString();
if (controller == "BackGroundTaskController" &&
action == "BackGroundTaskAction")
{
// ignore
return;
}
HttpContext.Current.Session["UserLastActive"] = DateTime.Now;
}
}
每次请求时,将执行以下代码。您可以将变量“UserLastActive”设置为会话对象,但周期性请求除外。然后可以检查用户的最后一次活动时间,并使会话结束。