示例#1
0
    /// <summary>
    /// Determines whether the action method selection is valid for the specified controller context.
    /// </summary>
    /// <param name="controllerContext">The controller context.</param>
    /// <param name="methodInfo">Information about the action method.</param>
    /// <returns>true if the action method selection is valid for the specified controller context; otherwise, false.</returns>
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }

        Func <NameValueCollection> formGetter;
        Func <NameValueCollection> queryStringGetter;

        ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formGetter, out queryStringGetter);

        var form        = formGetter();
        var queryString = queryStringGetter();

        var req = form.AllKeys.Any() ? form : queryString;

        if (!string.IsNullOrEmpty(this.ActionRegex))
        {
            foreach (var key in req.AllKeys.Where(k => k.StartsWith(Action, true, System.Threading.Thread.CurrentThread.CurrentCulture)))
            {
                if (key.Contains(":"))
                {
                    if (key.Split(':').Count() == this.ActionRegex.Split(':').Count())
                    {
                        bool match = false;
                        for (int i = 0; i < key.Split(':').Count(); i++)
                        {
                            if (Regex.IsMatch(key.Split(':')[0], this.ActionRegex.Split(':')[0]))
                            {
                                match = true;
                            }
                            else
                            {
                                match = false;
                                break;
                            }
                        }

                        if (match)
                        {
                            return(!string.IsNullOrEmpty(req[key]));
                        }
                    }
                }
                else
                {
                    if (Regex.IsMatch(key, this.Action + this.ActionRegex))
                    {
                        return(!string.IsNullOrEmpty(req[key]));
                    }
                }
            }
            return(false);
        }
        else
        {
            return(req.AllKeys.Contains(this.Action));
        }
    }
示例#2
0
        public void Prepare(ILogEvent logEvent)
        {
            var context = HttpContext.Current;

            if (context == null || IsRequestResponseHidden(context))
            {
                return;
            }

            var request = context.Request;

            if (request == null)
            {
                return;
            }

            Func <NameValueCollection> formGetter;
            Func <NameValueCollection> queryStringGetter;

            ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formGetter, out queryStringGetter);

            var form = formGetter();

            logEvent.Set(KEY_REQUEST_ID, request.GetRequestId());
            logEvent.Set(KEY_REQUEST_IP, request.GetClientAddress());
            logEvent.Set(KEY_REQUEST_METHOD, request.HttpMethod);
            logEvent.Set(KEY_REQUEST_URL, request.Url);
            logEvent.Set(KEY_REQUEST_REFERRER, request.UrlReferrer);
            logEvent.Set(KEY_REQUEST_AGENT, request.UserAgent);

            logEvent.Set(KEY_REQUEST_COOKIES, string.Join("; ", request.Cookies.AllKeys.Select(k => string.Format("{0} = '{1}'", k, request.Cookies[k].Value))));
            logEvent.Set(KEY_REQUEST_POST, string.Join("; ", form.AllKeys.Select(k => string.Format("{0} = '{1}'", k, CensorePasswords && k.ToUpperInvariant().Contains("PASSWORD") ? "*******" : form[k]))));
        }
示例#3
0
        private void ResolveUnvalidatedCollections()
        {
            Func <NameValueCollection> formGetter, queryGetter;

            ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formGetter, out queryGetter);
            _form        = formGetter();
            _queryString = queryGetter();
        }
        public static UnvalidatedRequestValues Unvalidated(this HttpRequest request)
        {
            Func <NameValueCollection> func;
            Func <NameValueCollection> func2;
            HttpContext current = HttpContext.Current;

            ValidationUtility.GetUnvalidatedCollections(current, out func, out func2);
            return(new UnvalidatedRequestValues(new HttpRequestWrapper(current.Request), func, func2));
        }
        public static UnvalidatedRequestValues Unvalidated(this HttpRequest request)
        {
            // We don't actually need the request object; we'll get HttpContext.Current directly.
            HttpContext context = HttpContext.Current;
            Func <NameValueCollection> formGetter;
            Func <NameValueCollection> queryStringGetter;

            ValidationUtility.GetUnvalidatedCollections(context, out formGetter, out queryStringGetter);

            return(new UnvalidatedRequestValues(new HttpRequestWrapper(context.Request), formGetter, queryStringGetter));
        }
示例#6
0
        public AspNetRequest(HttpRequestBase request)
        {
            _request = request;
            Cookies  = new NameValueCollection();
            foreach (string key in request.Cookies)
            {
                Cookies.Add(key, request.Cookies[key].Value);
            }

            // Since the ValidationUtility has a dependency on HttpContext (not HttpContextBase) we
            // need to check if we're out of HttpContext to preserve testability.
            if (HttpContext.Current == null)
            {
                _form        = _request.Form;
                _queryString = _request.QueryString;
            }
            else
            {
                Func <NameValueCollection> formGetter, queryGetter;
                ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formGetter, out queryGetter);
                _form        = formGetter();
                _queryString = queryGetter();
            }
        }
示例#7
0
        public object GetData(HttpContextBase context)
        {
            var request = context.Request;

            var cookies = new List <object[]>
            {
                new[] { "Name", "Path", "Secure", "Value" }
            };

            var requestCookies = request.Cookies;

            foreach (var key in request.Cookies.AllKeys)
            {
                var cookie = requestCookies[key];

                if (cookie != null)
                {
                    cookies.Add(new[] { cookie.Name, cookie.Path, cookie.Secure.ToString(), context.Server.UrlDecode(cookie.Value) });
                }
            }

            //To avoid validation exceptions, input values are read like this
            Func <NameValueCollection> formData;
            Func <NameValueCollection> queryStringData;

            ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formData, out queryStringData);//HACK: ugly hack to use HttpContext.Current because GetUnvalidatedCollections does not take in an HttpContextBase

            var form        = formData().Flatten();
            var querystring = queryStringData().Flatten();

            //make sure there is request data
            if (form == null && querystring == null && cookies.Count <= 1)
            {
                return(null);
            }

            //read input stream
            var inputStream = context.Request.InputStream;

            inputStream.Position = 0;
            var    inputString       = new StreamReader(inputStream).ReadToEnd();
            object inputStreamResult = string.IsNullOrEmpty(inputString) ? null : inputString;

            //If input was json, deserialize to object so it can be rendered later.
            if (context.Request.ContentType.StartsWith(@"application/json", StringComparison.OrdinalIgnoreCase))
            {
                inputStreamResult = JsonConvert.DeserializeObject(inputString);
            }

            return(new
            {
                Cookies = cookies,
                Form = form,
                QueryString = querystring,
                InputStream = inputStreamResult,
                Thread.CurrentThread.CurrentUICulture,
                request.ApplicationPath,
                request.AppRelativeCurrentExecutionFilePath,
                request.CurrentExecutionFilePath,
                request.FilePath,
                request.Path,
                request.PathInfo,
                request.PhysicalApplicationPath,
                request.PhysicalPath,
                request.RawUrl,
                Url = request.Url.ToString(),
                UrlReferrer = request.UrlReferrer == null ? null : request.UrlReferrer.ToString(),
                request.UserAgent,
                request.UserHostAddress,
                request.UserHostName
            });
        }