当 method =post时,读取inputStream到RequestBody

        /// <summary>
        /// 当 method =post时,读取inputStream到RequestBody
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!this.ModelState.IsValid) {
                filterContext.Result = Json(new 
                {
                    message = ModelStateHelper.GetModelErrorMessages(ModelState), 
                    code = ResponseCode.错误请求 
                });
                return;
            }
            if (Request.HttpMethod.Equals("POST")) {
                var pos = Request.InputStream.Position;
                Request.InputStream.Position = 0;
                var ln = Request.InputStream.Length;
                using (var sr = new StreamReader(Request.InputStream))
                {
                    this.RequestBody = sr.ReadToEnd();
                    ln = Request.InputStream.Length;
                    Request.InputStream.Position = pos;
                    logger.Info("RequestBody:" + RequestBody);
                }
                if (!Signature())
                {
                    filterContext.Result = Json(new
                    {
                        code = ResponseCode.错误请求,
                        message = "认证失败!",
                    });
                }
            }
        }

 

posted @ 2020-12-24 11:32  竹林听雨行  阅读(378)  评论(0编辑  收藏  举报