HTML | CSS | JavaScript | ASP.NET | PHP | C# | Java | VB.NET | MS SQL | MySQL | Flash
Detecting Session Timeouts using a ASP.Net MVC Action Filter
Some time ago, I read an article on how to detect a session timeout in a ASP.Net Web forms application by placing some code in a Base Page. The code worked for me and you could read more about it here. In my last post I mentioned that Microsoft released a beta version of their MVC implementation of ASP.Net and since them, I have been playing around with it. But, as with Web forms, I still needed a way to detect a session timeout and perform an action when a timeout is detected. So, I decided to use the same code implemented in the original article and implement it into my MVC project. I decided to write a custom action filter and apply it to my controller actions where I need to ensure that my session was still active. Here is the code for my action filter.
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 );
}
}
}
And then, I would apply this filter to my Controller action methods like so:
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();
}
}
}
The code is pretty straightforward. I just apply the [SessionExpireFilter] attribute on each method where I want to ensure that our session is still valid. If you have any questions, leave a comment under this post. Until next time…
Happy Coding!
| Print article | This entry was posted by admin on November 23, 2008 at 5:57 pm, and is filed under ASP.NET, MVC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Hey! I like your post “Detecting Session Timeouts using a ASP.Net MVC Action Filter” so well that I like to ask you whether I should translate into German and linking back. Greetings Engel
about 1 year ago
That’s fine. Sorry, I took so long to respond. You got caught in my SPAM filter.
about 1 year ago
This code is really helpful – thanks a lot.
Is there a reason why you used HttpContext.Current instead of filterContext.HttpContext? I’m implementing this and would like to make sure all my ducks are in a row, per se.
Thanks again.
about 1 year ago
I think using the filterContext.HttpContext would be better. At the time I made the posting, I actually looked to see if the filterContext had that property and either I overlooked it or it just wasn’t there in the Beta version. Nice catch. Thanks.
about 1 year ago
Nice post. I was just looking for that, thanks!
about 1 year ago
Cool! I was just looking for that too!!
about 11 months ago
Consider assigning a RedirectToRouteResult to filterContext.Result instead of manipulating the Response directly.
about 9 months ago
Thanks Tyrone Davis. Really very helpfull.
about 9 months ago
about 3 months ago
Problem when rendering the partialview.
I’m calling action from javascript, that action returns partial view and assigining the to some div.
If session expires, the page what we have mentioned in respone.redirect is returing as a partialview .
Please help me to solve this problem
about 3 months ago
Hi!!! i’m having a problem with your method, when it executes it adds “(S(44krko45it3muqqdcsnh0445))” to my qerystring!!! wht’s happening????
thanks, Nicole.
about 3 months ago
Looks like you may have cookieless sessions turned on in your web.config. Make sure your sessionState element under the system.web section have cookieless=”false” set for that element.
about 2 months ago
I’m also having the same issue as Mr. from post #10. Since this all originates from an Ajax call, if a session times out the calling page loads the login screen into the existing page.
Is there an easy way to do a total redirect?