MVC Update User Online via Action Filter
Action filter to update the currently logged in users last online time to allow you to identify which of your users are online
/// <summary>
/// User Online Action Filter Attribute
/// </summary>
public class UserOnlineActionFilterAttribute : ActionFilterAttribute
{
/// <summary>
/// Called by the ASP.NET MVC framework after the action method executes.
/// </summary>
/// <param name="filterContext">The filter context.</param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
try
{
if (filterContext.HttpContext != null && filterContext.HttpContext.User != null && filterContext.HttpContext.User.Identity != null)
{
Membership.Provider.GetUser(filterContext.HttpContext.User.Identity.Name, true);
}
}
catch
{
// BAD: Swallow the exception to prevent it messing with the users action
}
base.OnActionExecuted(filterContext);
}
}
Add the action filter to the global list inside the global.asax Application_Start.
GlobalFilters.Filters.Add(new UserOnlineActionFilterAttribute());






John 3:53 pm on March 22, 2011 Permalink |
Thanks for this post! I ran into the same problem. This seems a little odd to me, have you figured out why MVC3 is exhibiting this behavior?
John 4:13 pm on March 22, 2011 Permalink |
Figured out what is going on… this serverfault answer is the perfect explanation:
http://serverfault.com/questions/137073/401-unauthorized-on-server-2008-r2-iis-7-5
In my case, I had a logon page calling Html.RenderAction(), and that action had an [Authorization] attribute.
Cheers.
John 4:17 pm on March 22, 2011 Permalink |
Sorry… [Authorize]
John 8:24 pm on March 22, 2011 Permalink |
Cheers for the update, I’ll have to take a closer look I also have numerous Html.RenderAction() that properly implemnt the [Authorize] attribute.
However I only noticed it since MVC3 RC, I don’t think it was in either of the previous of the beta versions.