Newtonsoft.Json.Linq.JObject 遍历验证每个属性内容
业务需求,拦截器验证每个请求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 遍历验证每个属性内容的更多相关文章
- 遍历Newtonsoft.Json.Linq.JObject
JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...
- 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 ...
- 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 ...
- 如何遍历newtonsoft.json的JObject里的JSON数据
这种问题,在网上搜,居然没有答案,又是一堆垃圾,连谷歌上都搜不到.老实说,我喜欢这边的工作环境,可以上谷歌,毕竟是大公司,有自己的VPN .某组织整天禁这个禁那个,去年居然连谷歌都禁了,丧心病狂至此, ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...
- Newtonsoft.Json 通过 JObject 读取 json对像 超简单
/* json 格式的字符串解析 格式化 { "input": { "size": 193156, "type": "image/ ...
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- 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 ...
- Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称
手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...
随机推荐
- windows如何删除服务
有时候软件被安装成了服务,但是卸载后如何删除无效的服务,这时候就不是普通用户就能删除了,这属于操作系统管理级别了,需要使用管理命令了. 管理员权限打开cmd,执行如下: C:\Windows\syst ...
- macOS 10.15 Catalina xxx.app已损坏,无法打开,你应该将它移到废纸篓解决方法
原文连接:https://www.macwk.com/article/mac-catalina-1015-file-damage 更新macOS 10.15 Catalina后,很多在10.14上可以 ...
- git向远程git仓库提交代码步骤详解
一.从远程仓库clone工程到本地 git clone -b 分支名称 http://10.1.1.11/service/tmall-service.git localDestDirectory l ...
- PMP 第8章错题总结
1.项目经理为项目的可交付成果定义验收标准.这些应记录在项目范围说明书2.项目的总体预算是“成本基准+管理储备”,成本基准里本身已包含了应急储备.工作包成本估算.活动成本估算3.范围基准包含----项 ...
- 上传下载execl
ajax上传execl + easyexecl解析execl <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- express脚手架重建node项目
安装express 和express-generator cnpm install express express-generator -g express demo1 创建demo1项目, 进入项目 ...
- 使用Mysql中的concat函数或正则匹配来快速批量生成用于执行的sql语句
背景介绍 今天需要给一张表里面补数据,需要按照行的维度进行update,如果是个别数据那么直接写update语句就可以了,但是场景要求的是将整表的数据进行update,要实现这个需求就不能只靠蛮力了, ...
- 配置git diff和git merge使用的第三方工具
一般在运行git merge branchName后,git 如果提示了merger冲突,然后运行git mergetool.Git提示冲突后,运行git mergetool --tool-help ...
- Invalid byte tag in constant pool: 19 与 javax/el/ELManager问题解决
整个生产环境采用JDK1.8 + Tomcat 7.0.65 代码重构后,启动报错,主要异常如下: 严重: Unable to process Jar entry [module-info.class ...
- [转帖]使用 Vagrant 打造跨平台开发环境
使用 Vagrant 打造跨平台开发环境 https://segmentfault.com/a/1190000000264347 Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/p ...