ASP.Net MVC 3: Where to handle session loss?

I began to run into errors when my session was lost, or when restoring my project, as my authentication cookie still lives.

In WebForms, I would use a master page associated with pages that require a login to simply verify the session.

How do I do this in one place in MVC? I would not like to check the state of the session in every action in my controllers.

On the other hand, I cannot just apply a global filter, since not all controllers require session state.

Perhaps this is possible in my layout? This is the only thing that pages requiring a session are general.

+5
source share
3

, , - , . , . . , , , .

+2

. , . :

protected void Session_Start(object src, EventArgs e)
    {
        if (Context.Session != null)
        {
            if (Context.Session.IsNewSession)
            {
                string sCookieHeader = Request.Headers["Cookie"];
                if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    // how to simulate it ???   
                    // RedirectToAction("ActionName", "ControllerName",  route values);  
                    Response.Redirect("/Home/TestAction");
                }

            }
        }


    }
+2

, , Global Filters . , , , , , . , , .

, - " ".

0

All Articles