Beispiel #1
0
        public async Task BindModelAsync(ModelBindingContext modelBinderContext)
        {
            var request = modelBinderContext.ActionContext.HttpContext.Request;

            if (request.Method != HttpMethods.Get)
            {
                bool isUnSecure       = false;
                var  isUnSecureHeader = request.Headers["X-isUnSecureSwagger"];
                if (!string.IsNullOrEmpty(isUnSecureHeader))
                {
                    bool.TryParse(isUnSecureHeader.ToString(), out isUnSecure);
                }

                using (var streamReader = new StreamReader(request.Body))
                {
                    var body = await streamReader.ReadToEndAsync();

                    var decryptBody = isUnSecure ? body : AesDescryptionHelper.Decrypt(body);

                    if (!modelBinderContext.ModelType.IsPrimitive && modelBinderContext.ModelType != typeof(decimal) && modelBinderContext.ModelType != typeof(string))
                    {
                        modelBinderContext.Result = ModelBindingResult.Success(
                            JsonConvert.DeserializeObject(decryptBody, modelBinderContext.ModelType));
                    }
                    else
                    {
                        modelBinderContext.Result = ModelBindingResult.Success(
                            modelBinderContext.ModelType == typeof(string) ? decryptBody : Convert.ChangeType(decryptBody, modelBinderContext.ModelType));
                    }
                }
            }
        }
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var request = bindingContext.ActionContext.HttpContext.Request;
            var values  = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (values.Length > 0)
            {
                bool isUnSecure       = false;
                var  isUnsecureHeader = request.Headers["isUnSecure"];
                if (!string.IsNullOrEmpty(isUnsecureHeader))
                {
                    bool.TryParse(isUnsecureHeader.ToString(), out isUnSecure);
                }

                var value = isUnSecure ? values.FirstValue : AesDescryptionHelper.Decrypt(WebUtility.UrlDecode(values.FirstValue.Replace('!', '%')));

                if (!bindingContext.ModelType.IsPrimitive && bindingContext.ModelType != typeof(decimal) && bindingContext.ModelType != typeof(string))
                {
                    bindingContext.Result = ModelBindingResult.Success(
                        JsonConvert.DeserializeObject(value, bindingContext.ModelType));
                }
                else
                {
                    bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelType == typeof(string) ? value : Convert.ChangeType(value, bindingContext.ModelType));
                }
            }

            await Task.CompletedTask;
        }
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            bool isUnSecure       = false;
            var  request          = bindingContext.ActionContext.HttpContext.Request;
            var  isUnsecureHeader = request.Headers["X-isUnSecureSwagger"];

            if (!string.IsNullOrEmpty(isUnsecureHeader))
            {
                bool.TryParse(isUnsecureHeader.ToString(), out isUnSecure);
            }

            var accessor = bindingContext.HttpContext.RequestServices.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;

            if (accessor.HttpContext.Request.Headers.ContainsKey(CommonConstant.PayloadHeaderName) || isUnSecure)
            {
                var payloadData = accessor.HttpContext.Request.Headers[CommonConstant.PayloadHeaderName];

                var payload = isUnSecure ? payloadData.ToString() : AesDescryptionHelper.Decrypt(payloadData).ToString();

                if (!bindingContext.ModelType.IsPrimitive && bindingContext.ModelType != typeof(decimal) && bindingContext.ModelType != typeof(string))
                {
                    bindingContext.Result = ModelBindingResult.Success(
                        JsonConvert.DeserializeObject(payload, bindingContext.ModelType));
                }
                else
                {
                    bindingContext.Result = ModelBindingResult.Success(
                        bindingContext.ModelType == typeof(string) ? payload : Convert.ChangeType(payload, bindingContext.ModelType));
                }
            }

            await Task.CompletedTask;
        }