将用户代理保存到数据库避免SQL注入?

问题描述:

我有一个动作过滤器,用于跟踪用户活动。我还将用户代理保存到数据库以查看我的网站最常访问的设备。我关心的是SQL注入,就好像我用我的浏览器来调整我的用户代理,我可以注入sql。有谁知道我可以如何过滤或验证这些用户代理字符串?将用户代理保存到数据库避免SQL注入?

ActionLog log = new ActionLog() 
     { 

      UserName = filterContext.HttpContext.User.Identity.Name, 
      Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, 
      Action = filterContext.ActionDescriptor.ActionName, 
      IP = filterContext.HttpContext.Request.UserHostAddress, 
      DateTime = filterContext.HttpContext.Timestamp, 
      UserAgent = filterContext.HttpContext.Request.UserAgent 
     }; 
     db.AddToActionLogs(log); 
     db.SaveChanges(); 

实体框架使用SQL paramters,所以基本上你几乎对SQL-注射保护(在你提供的代码示例中leats)