C# 浏览器关闭时会话超时
我正在使用以下代码检测会话到期:C# 浏览器关闭时会话超时,c#,asp.net-mvc,session,session-timeout,C#,Asp.net Mvc,Session,Session Timeout,我正在使用以下代码检测会话到期: public class SessionActionFilterAttribute : ActionFilterAttribute { /// <summary>Called by the ASP.NET MVC framework before the action method executes.</summary> /// <param name="filterContext">The filter co
public class SessionActionFilterAttribute : ActionFilterAttribute
{
/// <summary>Called by the ASP.NET MVC framework before the action method executes.</summary>
/// <param name="filterContext">The filter context.</param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// The following code is used for checking if a session has timed out. The default timeout value for ASP.NET is 20mins.
// The timeout value can be overriden in the Web.config file using the sessionState tag's timeout attribute.
// <sessionState timeout="5"></sessionState>
// Check for an existing session.
if (null != filterContext.HttpContext.Session)
{
// Check if we have a new session.
// IsNewSession cannot discern between: is it a new visitor with fresh session, or an existing visitor with expired session.
if (filterContext.HttpContext.Session.IsNewSession)
{
string cookieHeaders = filterContext.HttpContext.Request.Headers["Cookie"];
// Check if session has timed out.
// Does session cookie exist, if so ASP.NET session is expired
if ((null != cookieHeaders) && (cookieHeaders.IndexOf("ASP.NET_SessionId") >= 0))
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
FormsAuthentication.SignOut();
}
// Redirect to login.
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Account" },
{ "action", "Index" },
{ "timeout", "True"}
});
return;
}
}
}
// Else continue with action as usual.
// Session is not expired and function will return false, could be new session, or existing active session
base.OnActionExecuting(filterContext);
}
}
公共类SessionActionFilterAttribute:ActionFilterAttribute
{
///在执行操作方法之前由ASP.NET MVC框架调用。
///过滤器上下文。
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
//以下代码用于检查会话是否超时。ASP.NET的默认超时值为20分钟。
//可以使用sessionState标记的timeout属性在Web.config文件中重写超时值。
//
//检查现有会话。
if(null!=filterContext.HttpContext.Session)
{
//检查是否有新的会话。
//IsNewSession无法区分以下两种情况:是具有新会话的新访问者,还是会话已过期的现有访问者。
if(filterContext.HttpContext.Session.IsNewSession)
{
字符串cookieHeaders=filterContext.HttpContext.Request.Headers[“Cookie”];
//检查会话是否超时。
//会话cookie是否存在,如果存在,ASP.NET会话是否已过期
if((null!=cookieHeaders)&&(cookieHeaders.IndexOf(“ASP.NET_SessionId”)>=0))
{
if(filterContext.HttpContext.Request.IsAuthenticated)
{
FormsAuthentication.SignOut();
}
//重定向到登录。
filterContext.Result=新的RedirectToRouteResult(
新RouteValueDictionary
{
{“控制器”、“帐户”},
{“操作”,“索引”},
{“超时”,“真”}
});
返回;
}
}
}
//否则就照常行动。
//会话未过期,函数将返回false,可能是新会话,也可能是现有活动会话
base.OnActionExecuting(filterContext);
}
}
在某种程度上,这很好用
当用户登录并在会话超时(未注销)前关闭浏览器时
然后尝试再次查看该站点,并在会话超时后重新登录。它会不断重定向到登录页面,即,上面的代码认为会话已持续过期,但我猜由于某种原因,cookie仍保持为“过期”
这里有我遗漏的东西吗
另外,我在web.config中使用了以下内容
<sessionState timeout="1"></sessionState>
Gah。。。。我在重定向之前添加了以下内容,似乎已经解决了这个问题。。。。只需再进行一点测试即可:
if (filterContext.HttpContext.Request.Cookies["ASP.NET_SessionId"] != null)
{
filterContext.HttpContext.Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddDays(-1);
}
filterContext.HttpContext.Session.Abandon();