Session TimeOut MVC 5

0

I'm trying to implement the Session Timeout in an MVC5 application.

I have the following string in Web.config:

<sessionState mode="InProc" cookieless="true" timeout="15" />

I created the class:

public class SessionExpireAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;
        // check  sessions here
        if( HttpContext.Current.Session["username"] == null ) 
        {
           filterContext.Result = new RedirectResult("~/Home/Index");
           return;
        }
        base.OnActionExecuting(filterContext);
    }
}

I added the tag to the class:

[SessionExpire]
public class HomeController : Controller
{
  public ActionResult Index()
  {
     return Index();
  }
}

In the login action I added:

Session["username"] = usuario.UserName;

The problem occurs when you run the code:

if( HttpContext.Current.Session["username"] == null ) 
        {
           filterContext.Result = new RedirectResult("~/Home/Index");
           return;
        }

This code always displays null for HttpContext.Current.Session ["username"]

    
asked by anonymous 04.08.2017 / 07:00

0 answers