Tracking user's activity in MVC3
One way for tracking user's activity in MVC3:
-
I write class that implements the base class Action FilterAttribute, called
UserTrackingActionFilterAttribute:public class UserTrackingActionFilterAttribute : ActionFilterAttribute {}
-
Override the method OnResultExecuting:
public override void OnResultExecuting(ResultExecutingContext context) { base.OnResultExecuting(context); }
-
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); }
-
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!!!
Written by Maria Manenti
Related protips
1 Response
Grazie Maria,
utilissimo. Inglese ottimo!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mvc
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#