我无法处理MVC 3中的会话过期检查, 最初我尝试过MVC过滤器(Action Executing)来检查会话,但我无法访问动作过滤器旁边的request.session变量,
我尝试在global.asax中使用session_Start方法,但问题是当会话过期时它不会转到sesion_start方法
答案 0 :(得分:0)
你可以像这样创建属性
public class SessionExpireFilterAttribute:ActionFilterAttribute
{
public override void OnActionExecuting( ActionExecutingContext filterContext ) {
HttpContext ctx = HttpContext.Current;
// check if session is supported
if(ctx.Request.IsAuthenticated)
{
if (ctx.Session != null)
{
// check if a new session id was generated
if (ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if (null == sessionCookie)
{
FormsAuthentication.SignOut();
const string loginUrl = @"~/Login/Login";
var rr = new RedirectResult(loginUrl);
filterContext.Result = rr;
}
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") <= 0))
{
FormsAuthentication.SignOut();
const string loginUrl = @"~/Login/Login";
var rr = new RedirectResult(loginUrl);
filterContext.Result = rr;
}
}
}
}
else
{
ctx.Response.Redirect(@"~/Login/Login");
}
base.OnActionExecuting ( filterContext );
}
}