ASP.Net MVC: Handling Forms Authentication Timeouts for AJAX Requests

Posted over 2 years ago on September 26, 2010

Problem: You need a way to redirect the user back to a login page when a user makes an AJAX request and their forms authentication session has timed out.

Solution: Add a HTTP header onto each AJAX response that indicates whether or not the user is still logged in and their session is active.

This is an example of how you might add a response header to each AJAX request.

using System.Web.Mvc;
using System.Web.Security;

namespace MVCAjaxFormsAuthTimeout.Helper
{
    public class CommunicateAuthStatusToAjaxClientAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if(filterContext.HttpContext.Request.IsAjaxRequest())
            {
                if(filterContext.HttpContext.User.Identity.IsAuthenticated)
                    filterContext.HttpContext.Response.Headers.Add("X_User_Logged_In", "true");
            }

            base.OnActionExecuted(filterContext);
        }
    }
}

Usage:

[CommunicateAuthStatusToAjaxClient]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }
    }

On the client side, I want every AJAX request made from jQuery to check this header.  Instead of touching every place where I make a call to the server, I use the ajaxSuccess(...) method to configure this once in the master page.

    
    

When the users' session has ended, the header variable will be empty and the alert and redirect will execute.

Source code is here.

Note: Modifying response headers requires "IIS Integrated Pipeline Mode" which the Cassini development server does not support.  Therefore, if you would like to see the code run you must configure IIS to handle the web project.

Central Ohio .Net Developers Group: jQuery

Posted over 4 years ago on October 23, 2008

I gave a very short 15 minute "Lightning Talk" on jQuery at the Central Ohio .Net Developers Group. Here is the demo project and slide deck:

jQuery Presentation (SkyDrive)

Alternating Element Highlighting With jQuery

Posted over 4 years ago on September 26, 2008

Reading tabular data is usually made a little easier by distinguishing alternating rows by a slightly different color.  Normally, to accomplish this I would just assign a different CSS style using a server control such as a GridView or Repeater.  While working today, I found a challenge in applying the alternating row effect because I was outputting a static HTML table with some dynamic rows thrown in the middle using a Repeater.  So depending on how many dynamic rows were rendered, sometimes I would get two rows by each other with the same color.

So anyway, the solution was to implement the alternating row coloring on the client using jQuery.  Here is the very simple code:

alternaterowhighlighting

You can even substitute the <tr> tag to any html container (div/span/etc.). 

jQuery rocks!!