Example #1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            SetIsTrapped(false);
            if (filterContext.HttpContext.Request.HttpMethod == "GET")
            {
                return;
            }

            var requestData = HttpContext.Current.Request.Form;

            if (requestData.Count == 0)
            {
                return;
            }

            foreach (string honeypotField in honeypots)
            {
                //Trap any field that is contained in the passed array of honeypotFields
                if (!String.IsNullOrWhiteSpace(requestData[honeypotField]))
                {
                    isTrapped = true;
                }
                //if not traped set original name before hashing and appopriate value
                else
                {
                    string hashedName = HtmlHelpers.GetHashedPropertyName(honeypotField);
                    if (requestData.AllKeys.Contains(hashedName))
                    {
                        string val = HttpContext.Current.Request.Form[hashedName];
                        foreach (var actionValue in filterContext.ActionParameters)
                        {
                            foreach (var prop in actionValue.Value.GetType().GetProperties())
                            {
                                if (prop.Name == honeypotField && prop.CanWrite && prop.PropertyType == typeof(string))
                                {
                                    if (prop.PropertyType == val.GetType())
                                    {
                                        prop.SetValue(actionValue.Value, val);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (IsTrapped)
            {
                SetIsTrapped(true);
                if (HoneypotSettings.Settings.BlockRequests)
                {
                    filterContext.HttpContext.Response.StatusCode = 403;
                }
            }
            LogRequest(HttpContext.Current.Request);
        }