业务需求,拦截器验证每个请求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. spring cloud java: 无法访问redis.clients.jedis.JedisPoolConfig 找不到redis.clients.jedis.JedisPoolConfig的类文件

    spring cloud <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 注入Redis 时候: @Bea ...

  2. Linux下 nohup后台运行springboot jar 包时,使用指定的 application.yml配置

    jar 包启动时指定配置文件 application.yml nohup java -jar -Dserver.port=8080 wx-member-card-0.0.1-SNAPSHOT.war ...

  3. Linux下查看占用CPU资源最多的几个进程

    linux下获取占用CPU资源最多的10个进程: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用内存资源最多的10个进 ...

  4. [Python] 项目的配置覆盖与合并

    参考来源: https://www.liaoxuefeng.com/wiki/1016959663602400/1018490750237280 代码稍微修改了一下 import os import ...

  5. Properties的有序读写

    使用java.util.Properties提供的类,读取properties文件的时候,读出来的是乱序的 如下边的情况 import java.io.*; import java.util.Arra ...

  6. mysql8.0 grant 创建账号及权限记录

    针对 42000错误 原文:https://stackoverflow.com/questions/50177216/how-to-grant-all-privileges-to-root-user- ...

  7. 说说Java Web中的Web应用程序|乐字节

    大家好,我是乐字节的小乐,今天接着上期文章<Javaweb的概念与C/S.B/S体系结构>继续往下介绍Java Web ,这次要说的是web应用程序. 1. Web 应用程序的工作原理 W ...

  8. pandas 模块

    什么是pandas pandas是一个python的包,主要用来处理表格格式的文件,可以快速的对表格进行查询,过滤,合并等操作. pandas的简单使用 pandas读入table格式文件 #读入一个 ...

  9. centos 7 下使用jexus 配置ASP.NET Core

    1.安装jexus Jexus独立版(专业版)的安装 Jexus“独立版”指的是自带.net运行时(mono),不需要在客户服务器安装mono就能正常运行的Jexus版本,该版本只支持 64位Linu ...

  10. Resouce Pool的理解

    本篇文章从现象到本质再到具象去理解 , 从理论到实战再到源码回顾去深化. 1.在开发中,无处不在的池. eg 网络通信连接池: HttpClient连接池 HttpClient通过PoolingHtt ...