Skip to main content
 首页 » 编程设计

c#之如何在 ASP.Net MVC 5 中跟踪匿名用户的事件

2024年02月05日10wayfarer

我创建了一个 ASP.Net MVC 5 网络应用程序,其服务可供匿名用户使用。当匿名用户使用网络服务时,它会从数据库中做一些查询。然而,出于安全原因,我的客户想要跟踪匿名用户的“可疑”事件。其中之一包括匿名用户每天查询的次数(以防止大量数据被“窃取”)。

有什么方法可以获取这些信息吗?

对于注册用户,我们可以在 ApplicationUser 中创建名为 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; 
    } 
} 

然后像这样简单地在 View 中使用它:

<p>No Of Query: @User.Identity.GetQueryNo()</p> 

但我们如何跟踪匿名用户的事件(即查询次数)?

请您参考如下方法:

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

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() 

或者在 Controller 级别:

[TrackingActionFilter] 
public class HomeController : Controller 

或者您可以通过 FilterConfig 全局覆盖整个 MVC 应用程序:

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
        filters.Add(new HandleErrorAttribute()); 
        filters.Add(new TrackingActionFilter()); 
    } 
}