如何在ASP.Net MVC 5中跟踪匿名用户的活动?

时间:2016-08-10 12:10:28

标签: c# asp.net asp.net-mvc asp.net-mvc-5 user-activity

我创建了一个ASP.Net MVC 5 Web应用程序,其服务可供匿名用户使用。当匿名用户使用Web服务时,它将从数据库执行一些查询。但是,出于安全原因,我的客户希望跟踪匿名用户的“可疑”活动。其中一个包括匿名用户每天查询的次数(防止大量数据“被盗”)。

有没有办法捕获这些信息?

对于注册用户,我们可以在名为ApplicationUser的{​​{1}}中创建其他属性,并将其添加到QueryNo中,如下所示:

Claim

当我们想跟踪其活动时,我们可以简单地增加每个查询活动的public class ApplicationUser : IdentityUser { public uint QueryNo { get; set; } //how many times this user has queried public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here userIdentity.AddClaim(new Claim("QueryNo", QueryNo)); return userIdentity; } } 。当我们想要显示它时,我们可以,例如,像这样简单地定义QueryNo的扩展名:

Identity

然后只需在视图中使用它:

public static class IdentityExtensions {
    public static string GetQueryNo(this IIdentity identity) {
        if (identity == null) {
            throw new ArgumentNullException("identity");
        }
        var ci = identity as ClaimsIdentity;
        if (ci != null) {
            return ci.FindFirstValue("QueryNo");
        }
        return null;
    }
}

但是我们如何跟踪匿名用户的活动(例如没有查询)?

1 个答案:

答案 0 :(得分:3)

首先创建您的动作过滤器:

public class TrackingActionFilter : ActionFilterAttribute
{

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var sessionId = filterContext.HttpContext.Session.SessionID;
        Debug.WriteLine("Printing session Id: " + sessionId);

        var ip = filterContext.HttpContext.Request.UserHostAddress;
        Debug.WriteLine("Printing ip: " + ip);

        var headers = filterContext.RequestContext.HttpContext.Request.Headers;
        foreach(var header in headers) {
            Debug.WriteLine("Printing header: " + header);
        }

        var parms = filterContext.HttpContext.Request.Params;
        foreach (var key in parms.AllKeys)
        {
            Debug.WriteLine("Printing parameter: " + key + " - " + parms[key]);
        }

        var routeDataKeys = filterContext.RouteData.Values.Keys;
        foreach(var key in routeDataKeys)
        {
            Debug.WriteLine("Printing route data value: " + key + " - " + filterContext.RouteData.Values[key]);
        }

        //Stolen with love from http://stackoverflow.com/questions/12938621/how-can-i-log-all-query-parameters-in-an-action-filter
        var stream = filterContext.HttpContext.Request.InputStream;
        var data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        Debug.WriteLine(Encoding.UTF8.GetString(data));
    }
}

很明显,您将捕获相关详细信息,而不是仅将其写入调试窗口。

现在,您可以在操作级别应用操作过滤器:

[TrackingActionFilter]
public ActionResult Index()

或在控制器级别:

[TrackingActionFilter]
public class HomeController : Controller

或者您可以通过FilterConfig

全局覆盖整个MVC应用程序
public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new TrackingActionFilter());
    }
}