Session Timeout MVC 5

Asked

Viewed 589 times

0

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

I have the following string on 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 executing 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"]

  • Just a question (it has nothing to do with the issue of Session there), but are you using ASP.NET Identity? He has mechanisms ready to take care of it for you.

  • I actually want to deal with Session. I have requests that I want to deal with after a while of inactivity. These are requests that are made via Jquery using ajax. So if you have spent time without the user having any activity, I want to force a new login.

1 answer

0

I have the same problem, when I run the command always returns NULL private ObjX Entidade = new ObjX(); Entidade = (ObjX)Session["SessaoObj"];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.