Last Updated: February 25, 2016
·
1.615K
· merys

Tracking user's activity in MVC3

One way for tracking user's activity in MVC3:

  1. I write class that implements the base class Action FilterAttribute, called
    UserTrackingActionFilterAttribute:

    public class UserTrackingActionFilterAttribute : ActionFilterAttribute
    {}
  2. Override the method OnResultExecuting:

    public override void OnResultExecuting(ResultExecutingContext   context)
    {
        base.OnResultExecuting(context);
     }
  3. In the method, I write the logic for store user's data. In my case, I have the repository (AccessesRepository) with a static method (WriteAccess), and I store ip address, url called, browser info:

     public override void OnResultExecuting(ResultExecutingContext context)
     {
             base.OnResultExecuting(context);
              new AccessesRepository().WriteAccess(
                                context.HttpContext.User.Identity.Name,                          HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.Url.ToString(), HttpContext.Current.Request.UserAgent);
    
    }
  4. Finally,I should register our filter into global.asax, into method Application_Start():

    protected void Application_Start()
     {
        AreaRegistration.RegisterAllAreas();
    
        GlobalFilters.Filters.Add(new UserTrackingActionFilterAttribute());
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
     }

It's very simple.. and my english so poor!!!

1 Response
Add your response

Grazie Maria,
utilissimo. Inglese ottimo!

over 1 year ago ·