【问题标题】:Redirect at Session Timeout in Global.asax in mvc4在 mvc4 中的 Global.asax 中的会话超时重定向
【发布时间】:2014-01-30 16:35:26
【问题描述】:

我正在尝试检测会话何时结束,然后在我的全局 asax 文件中完成此操作后将用户重定向到主页。

我正在使用以下代码,我发现 here

global.asax:

protected void Session_Start()
    {
        if (Context.Session != null)
        {
            if (Context.Session.IsNewSession)
            {
                string sCookieHeader = Request.Headers["Cookie"];
                if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    //intercept current route
                    HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
                    RouteData routeData = RouteTable.Routes.GetRouteData(currentContext);


                    //Substitute route Data Token Values for the Area
                    routeData.DataTokens["area"] = "";
                    routeData.DataTokens["UseNamespaceFallback"] = true;


                    //substitute route values
                    routeData.Values["controller"] = "home";
                    routeData.Values["action"] = "index";
                    routeData.Values.Add("timedOut", "true");
                    //routeData.Values["id"] = "timedOut";

                    IRouteHandler routeHandler = routeData.RouteHandler;
                    RequestContext requestContext = new RequestContext(currentContext, routeData);

                    IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
                    httpHandler.ProcessRequest(Context);

                    Response.Flush();
                    Response.End();
                }
            }
        }
    }

我认为它可以在开发环境中工作,但是当我在我的服务器 (IIS7) 上尝试它时,我收到以下错误。

“HttpContext.SetSessionStateBehavior”只能在“HttpApplication.AcquireRequestState”之前调用

我已经使用here 之类的链接发现了这个问题,但我就是无法让它工作。我认为问题出在以下几行

IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
                    httpHandler.ProcessRequest(Context);

但是我似乎无法让它在服务器上运行。有什么想法或建议吗?

【问题讨论】:

  • 服务器端会话结束时,连接没有用,所以会话端不能对用户做任何事情。您能做的最好的事情就是在页面中放置一个与会话超时匹配的计时器。

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 session-timeout


【解决方案1】:

你可以为你的控制器创建一个自定义的动作过滤器来处理这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;


namespace Web {


    public class SessionExpireFilterAttribute : ActionFilterAttribute {


        public override void OnActionExecuting( ActionExecutingContext filterContext ) {
            HttpContext ctx = HttpContext.Current;


            // check if session is supported
            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 ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) ) {


                        ctx.Response.Redirect ( "~/Home/Login" );
                    }
                }
            }


            base.OnActionExecuting ( filterContext );
        }
    }
}

然后,我会将此过滤器应用于我的控制器操作方法,如下所示:

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;

    namespace Web.Controllers {

        public class HomeController : Controller {

            [SessionExpireFilter]
            public ActionResult Index( ) {
                // This method will not execute if our session has expired

                // render Home Page
                return View();
            }

            public ActionResult Login() {
                // render Login page
                return View();
            }
        }
    }

【讨论】:

  • 我一直在努力寻找与 OP 相同的问题的解决方案。我在这个网站上找到的几个答案与您发布的内容类似,在当前会话实际到期之前都可以正常工作。如果您在一个页面上并且会话过期,它甚至不会命中属性,因此它会重定向。我发现其他人也有同样的问题..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多