业务需求,拦截器验证每个请求inputstream(实际是application/json流)的数据,但是json反序列化实体格式不同。

            var req = filterContext.RequestContext.HttpContext.Request;
if (req.ContentType.ToLower().Contains("application/json") && req.InputStream.Length > )
{
System.IO.Stream stm = new MemoryStream();
req.InputStream.CopyTo(stm);
stm.Position = ;
req.InputStream.Position = ;
using (System.IO.StreamReader sr = new System.IO.StreamReader(stm))
{
try
{
Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Parse(sr.ReadToEnd());
if (jo.HasValues)
{
foreach (JToken item in jo.Values())
{
var tmpMsg = "";
int ckResult = ChkJson(item, out tmpMsg);
if (ckResult != )
{
Content.Content = tmpMsg;
filterContext.Result = Content;
filterContext.HttpContext.Response.StatusCode = ckResult;
filterContext.HttpContext.Response.StatusDescription = "sensitive information";
return;
}
}
}
}
catch (System.Exception)
{
// 若输入流不是json对象不再校验
} }
}
        protected new int ChkJson(JToken jo, out string msg)
{
msg = "";
if (jo == null) return ;
if (jo.HasValues && jo.Values().Count() > )
{
foreach (JToken item in jo.Values())
{
var result = ChkJson(item, out msg);
if (result != )
return result;
}
}
else
{
string val = jo.ToString();
if (IsContainXSSCharacter(val , out msg)){
return ;
}
} return ;
}

Newtonsoft.Json.Linq.JObject 遍历验证每个属性内容的更多相关文章

  1. 遍历Newtonsoft.Json.Linq.JObject

    JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...

  2. Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.

    https://blog.csdn.net/zhouyingge1104/article/details/83307637 C#项目中使用NewtonSoft.json,报错提示: Can not a ...

  3. asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported

    在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq ...

  4. 如何遍历newtonsoft.json的JObject里的JSON数据

    这种问题,在网上搜,居然没有答案,又是一堆垃圾,连谷歌上都搜不到.老实说,我喜欢这边的工作环境,可以上谷歌,毕竟是大公司,有自己的VPN .某组织整天禁这个禁那个,去年居然连谷歌都禁了,丧心病狂至此, ...

  5. Newtonsoft.Json.Linq

    var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...

  6. Newtonsoft.Json 通过 JObject 读取 json对像 超简单

    /* json 格式的字符串解析 格式化 { "input": { "size": 193156, "type": "image/ ...

  7. Newtonsoft.Json.Linq对象读取DataSet数据

    Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e)        {   ...

  8. Newtonsoft.Json.Linq 常用方法总结

    目录 1.Entity to Json 1.1.准备工作 1.2.Entity to Json 1.3.Json to Entity 2.Linq To Json 2.1.创建对象 2.2.从 Jso ...

  9. Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

随机推荐

  1. apache Request-URI Too Large 处理办法

    在Apache的httpd.conf配置文件中(直接加就可以) LimitRequestLine 40940 LimitRequestFieldSize 40940

  2. k8s记录-kubeam方式部署k8s

    参考:https://blog.csdn.net/networken/article/details/84991940 # k8s工具部署方案 # 1.集群规划 | **服务器** | || ---- ...

  3. pytorch使用DataParallel并行化负载不均衡问题

    使用DataParallel进行并行化时的结构如下: 在上图第一行第四个步骤中,GPU-1 其实汇集了所有 GPU 的运算结果.这个对于多分类问题还好,但如果是自然语言处理模型就会出现问题,导致 GP ...

  4. laydate.render报错:日期格式不合法

    在使用laydate渲染日期时: laydate.render({ elem: '#day' }); 提示日期格式不合法 需要使用 too.dateType()来包装 <input type=& ...

  5. 【计算机视觉基础】IPM

    IPM code #if 0 void xyp2ipmp(cv::Mat& xyp, cv::Mat& ipmp, cv::Mat& xylim, Size& sz){ ...

  6. 使用ReadtheDocs托管技术文档

    ReadtheDocs Read the Docs非常适合写软件文档以及编写一些教程.电子书之类.对于一些一两篇文章就能写清楚的可以记笔记或写博客, 但是如果要写成一个系列的,不如写成一本书的形式,更 ...

  7. 使用Docker快速搭建Zookeeper和kafka集群

    使用Docker快速搭建Zookeeper和kafka集群 镜像选择 Zookeeper和Kafka集群分别运行在不同的容器中zookeeper官方镜像,版本3.4kafka采用wurstmeiste ...

  8. 【C++/C】指针基本用法简介-A Summary of basic usage of Pointers.

    基于谭浩强老师<C++程序设计(第三版)>做简要Summary.(2019-07-24) 一.数组与指针 1. 指针数组 一个数组,其元素均为指针类型数据,该数组称为指针数组.(type_ ...

  9. LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP

    题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...

  10. Oracle打印输出在控制台

    SET SERVEROUTPUT ON  --必须有,不然显示不出declare LN_C number(10,0):=0;begin DECLARE LS_STR1 VARCHAR2(200); - ...