Esempio n. 1
0
        Task <JToken> GetFormJTokenAsync
        (
            this ModelBindingContext target
        )
        {
            JToken r = null;
            var    formCollectionModelBinder =
                new FormCollectionModelBinder
                (
                    NullLoggerFactory
                    .Instance
                );
            await
            formCollectionModelBinder
            .BindModelAsync(target);

            if
            (
                target
                .Result
                .IsModelSet
            )
            {
                r = JTokenWebHelper
                    .ToJToken
                    (
                    (IFormCollection)
                    target
                    .Result
                    .Model
                    );
            }
            return(r);
        }
Esempio n. 2
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var request = bindingContext
                          .HttpContext
                          .Request;
            JToken jToken = null;

            async void RequestBodyProcess()
            {
                if (request.HasFormContentType)
                {
                    var formBinder = new FormCollectionModelBinder();
                    await formBinder.BindModelAsync(bindingContext);

                    if (bindingContext.Result.IsModelSet)
                    {
                        jToken = JTokenWebHelper
                                 .ToJToken
                                 (
                            (IFormCollection)
                            bindingContext
                            .Result
                            .Model
                                 );
                    }
                }
                else
                {
                    //if (request.IsJsonRequest())
                    {
                        using (var streamReader = new StreamReader(request.Body))
                        {
                            var   task = streamReader.ReadToEndAsync();
                            await task;
                            var   json = task.Result;
                            if (!json.IsNullOrEmptyOrWhiteSpace())
                            {
                                jToken = JToken.Parse(json);
                            }
                        }
                    }
                }
            }

            void RequestHeaderProcess()
            {
                var qs = request.QueryString.Value;

                qs = HttpUtility
                     .UrlDecode
                     (
                    qs
                     );
                qs = qs.TrimStart('?');
                var isJson = false;

                try
                {
                    jToken = JToken.Parse(qs);
                    isJson = jToken is JObject;
                }
                catch
                {
                }
                if (!isJson)
                {
                    jToken = request.Query.ToJToken();
                }
            }

            if
            (
                string.Compare(request.Method, "get", true) == 0
            )
            {
                RequestHeaderProcess();
                if (jToken == null)
                {
                    RequestBodyProcess();
                }
            }
            else
            //if
            //(
            //    string.Compare(request.Method, "get", true) == 0
            //)
            {
                RequestBodyProcess();
                if (jToken == null)
                {
                    RequestHeaderProcess();
                }
            }
            bindingContext
            .Result =
                ModelBindingResult
                .Success
                (
                    jToken
                );
        }
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var request = bindingContext
                          .HttpContext
                          .Request;

            JToken jToken = null;

            async void RequestFormBodyProcess()
            {
                if (request.HasFormContentType)
                {
                    var formCollectionModelBinder = new FormCollectionModelBinder(NullLoggerFactory.Instance);
                    await formCollectionModelBinder.BindModelAsync(bindingContext);

                    if (bindingContext.Result.IsModelSet)
                    {
                        jToken = JTokenWebHelper
                                 .ToJToken
                                 (
                            (IFormCollection)
                            bindingContext
                            .Result
                            .Model
                                 );
                    }
                }
                else
                {
                    //if (request.IsJsonRequest())
                    {
                        using (var streamReader = new StreamReader(request.Body))
                        {
                            var   task = streamReader.ReadToEndAsync();
                            await task;
                            var   json = task.Result;
                            if (!json.IsNullOrEmptyOrWhiteSpace())
                            {
                                jToken = JToken.Parse(json);
                            }
                        }
                    }
                }
            }

            void RequestQueryStringHeaderProcess()
            {
                var qs = request.QueryString.Value;

                if (qs.IsNullOrEmptyOrWhiteSpace())
                {
                    return;
                }
                qs = HttpUtility
                     .UrlDecode
                     (
                    qs
                     );
                if (qs.IsNullOrEmptyOrWhiteSpace())
                {
                    return;
                }
                qs = qs.TrimStart('?');
                if (qs.IsNullOrEmptyOrWhiteSpace())
                {
                    return;
                }
                var isJson = false;

                try
                {
                    jToken = JToken.Parse(qs);
                    isJson = jToken is JObject;
                }
                catch
                {
                }
                if (!isJson)
                {
                    jToken = request.Query.ToJToken();
                }
            }

            // 取 jwtToken 优先级顺序:Header → QueryString → Body
            StringValues   jwtToken      = string.Empty;
            IConfiguration configuration =
                (IConfiguration)request
                .HttpContext
                .RequestServices
                .GetService
                (
                    typeof(IConfiguration)
                );
            var jwtTokenName = configuration
                               .GetSection("TokenName")
                               .Value;
            var needExtractJwtToken = !jwtTokenName.IsNullOrEmptyOrWhiteSpace();

            void ExtractJwtTokenInJToken()
            {
                if (needExtractJwtToken)
                {
                    if (jToken != null)
                    {
                        if (StringValues.IsNullOrEmpty(jwtToken))
                        {
                            var j = jToken[jwtTokenName];
                            if (j != null)
                            {
                                jwtToken = j.Value <string>();
                            }
                        }
                    }
                }
            }

            if (needExtractJwtToken)
            {
                request
                .Headers
                .TryGetValue
                (
                    jwtTokenName
                    , out jwtToken
                );
            }
            RequestQueryStringHeaderProcess();
            ExtractJwtTokenInJToken();
            if
            (
                string.Compare(request.Method, "post", true) == 0
            )
            {
                RequestFormBodyProcess();
                ExtractJwtTokenInJToken();
                //if (jToken == null)
                //{
                //    RequestHeaderProcess();
                //}
            }
            if (!StringValues.IsNullOrEmpty(jwtToken))
            {
                request
                .HttpContext
                .Items
                .Add
                (
                    jwtTokenName
                    , jwtToken
                );
            }
            bindingContext
            .Result =
                ModelBindingResult
                .Success
                (
                    jToken
                );
        }