如何处理会话超时在MVC 3 [英] How to handle Session timeout in MVC 3

查看:208
本文介绍了如何处理会话超时在MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有频繁的会话超时问题。

I am having issues with frequent Session Time Out.

我想写我可以每个控制器上使用共同的过滤器,过滤器应该将用户重定向到登录,登录回从那里用户发送的最后一个请求后。

I want to write a common filter that I could use on each controller, filter should redirect the user to login and after log in back to from where user sent the last request.

推荐答案

您可以尝试这样的事:

public class SessionExpireAttribute : ActionFilterAttribute {
    public override void OnActionExecuted(ActionExecutedContext filterContext) {
        base.OnActionExecuted(filterContext);
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        if (filterContext.HttpContext.Session != null) {
            if (filterContext.HttpContext.Session.IsNewSession) {
                var sessionCookie = filterContext.HttpContext.Request.Headers["Cookie"];
                if ((sessionCookie != null) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) {
                    // redirect to login
                }
            }
        }
    }
}

这篇关于如何处理会话超时在MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆