json.net

ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统

杀马特。学长 韩版系。学妹 提交于 2020-08-16 16:02:44
升级到 Asp.Net Core 2.0 (2017/08/29 更新) 为什么使用 Jwt 最近,移动开发的劲头越来越足,学校搞的各种比赛都需要用手机 APP 来撑场面,所以,作为写后端的,很有必要改进一下以往的基于 Session 的身份认证方式了,理由如下: 移动端经常要保持长时间(1 到 2 星期)在线,但是 Session 却不好在服务端保存这么久,虽然可以持久化到数据库,但是还是挺费资源 移动端往往不是使用的网页技术,所以藏在 Cookie 里面的 SessionId 不是很方便的传递给服务端 服务端暴露给客户端的接口往往是 RESTful 风格的,这是一种无状态的 API 风格,所以身份认证的方式最好也是无状态的才好 所以我选择了使用 Jwt (Json Web Token) 这个技术。Jwt 是一种无状态的分布式的身份验证方式,与 Session 相反,Jwt 将用户信息存放在 Token 的 payload 字段保存在客户端,通过 RSA 加密的方式,保证数据不会被篡改,验证数据有效性。 下面是一个使用 Jwt 的系统的身份验证流程: 可以看出,用户的信息保存在 Token 中,而 Token 分布在用户的设备中,所以服务端不再需要在内存中保存用户信息了 身份认证的 Token 传递时以一种相当简单的格式保存在 header 中,方便客户端对其进行操作 Jwt

dotnet C# 如何让 Json 序列化数组时序列化继承类的属性

橙三吉。 提交于 2020-08-15 05:47:16
如果我使用的是具体的数组而我的数组是基类数组,而我传入子类的元素进行 json 序列化,可能发现 Json.NET 序列化没有包含子类元素的属性。如果要包含子类的属性或字段,可以在序列化的类数组定义为 object 数组的方式 我在用 WPF 写一个复杂的应用,我需要 ASP.NET Core 后台传输一个 AppData 类的数组,包含的属性如下 public class Lindexi { public string Name { set; get; } } 然后我有 Foo 类继承 Lindexi 类 public class Foo : Lindexi { public string F1 { set; get; } } 用下面代码序列化 static void Main(string[] args) { Console.WriteLine(ToString(new Foo() { F1 = "林德熙是逗比" })); } static string ToString(Lindexi lindexi) { return JsonSerializer.Serialize(new [] { lindexi }); } 运行可以看到输出 [{"Name":null}] 也就是 Foo 的属性被丢失了,在 .NET Core 3.0 可以使用 System.Text.Json

Deserialize inconsistent JSON property

╄→尐↘猪︶ㄣ 提交于 2020-08-09 08:02:11
问题 Hopefully someone can help me with the following inconsistency occurring in a large JSON file that I am attempting to deserialize using Newtonsoft.Json. One of the properties of the object occasionally appears as: "roles": [ { "field1" : "value", "field2" : "value" } ] While other times that same property appears as: "roles": { "roles": [ { "field1" : "value", "field2" : "value" } ] } For reference, this property is implemented in its class as: [JsonProperty("roles")] public List<Role> Roles

NewtonSoft JArray - how to select multiple elements with LINQ

久未见 提交于 2020-08-08 07:21:57
问题 I have some JSON which I then parse to JArray object. I want to filter the JArray so it then has only two properties, Title and BodyText. But whatever I try I can only select one value using LINQ. [HttpGet] public JsonResult AjaxGetNewsItems() { string json = JsonConvert.SerializeObject(news.GetNewsItems(), formatting:Formatting.Indented); var v = JArray.Parse(json); //var items = // v.Where( // x => // x["Title"].ToString() != string.Empty && // x["BodyText"].ToString() != string.Empty) //

NewtonSoft JArray - how to select multiple elements with LINQ

ぃ、小莉子 提交于 2020-08-08 07:20:11
问题 I have some JSON which I then parse to JArray object. I want to filter the JArray so it then has only two properties, Title and BodyText. But whatever I try I can only select one value using LINQ. [HttpGet] public JsonResult AjaxGetNewsItems() { string json = JsonConvert.SerializeObject(news.GetNewsItems(), formatting:Formatting.Indented); var v = JArray.Parse(json); //var items = // v.Where( // x => // x["Title"].ToString() != string.Empty && // x["BodyText"].ToString() != string.Empty) //

How can I increase the JSON deserialization MaxDepth limit in my ASP.NET Core webapp

╄→尐↘猪︶ㄣ 提交于 2020-08-07 07:50:52
问题 We're using ASP.NET Core 2.1 with .NET Framework 4.6.2. We have a customer who needs to send up a rather largely nested json structure to our webapp. When they make this call, we're outputting the following log and returning an error: The reader's MaxDepth of 32 has been exceeded. Path ' super.long.path.to property ', line 1, position 42111." I've looked through the ASP.NET Core codebase, and have observed a couple of references to MaxDepth = 32 in the deserializer provided with the framework

How can I increase the JSON deserialization MaxDepth limit in my ASP.NET Core webapp

我与影子孤独终老i 提交于 2020-08-07 07:49:56
问题 We're using ASP.NET Core 2.1 with .NET Framework 4.6.2. We have a customer who needs to send up a rather largely nested json structure to our webapp. When they make this call, we're outputting the following log and returning an error: The reader's MaxDepth of 32 has been exceeded. Path ' super.long.path.to property ', line 1, position 42111." I've looked through the ASP.NET Core codebase, and have observed a couple of references to MaxDepth = 32 in the deserializer provided with the framework

Newtonsoft.Json高级用法

人盡茶涼 提交于 2020-08-06 08:27:39
  手机端应用讲究速度快,体验好。刚好手头上的一个项目服务端接口有性能问题,需要进行优化。在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数据,经过分析一个简单的列表接口每一行数据返回了16个字段,但是手机APP端只用到了其中7个字段,剩余9个字段的数据全部都是多余的,如果接口返回数据为40K大小,也就是说大约20K的数据为无效数据,3G网络下20K下载差不多需要1s,不返回无效数据至少可以节约1s的时间,大大提高用户体验。本篇将为大家介绍Newtonsoft.Json的一些高级用法,可以修改很少的代码解决上述问题。 阅读目录 Newtonsoft.Json介绍 基本用法 高级用法 总结 回到顶部 Newtonsoft.Json介绍   在做开发的时候,很多数据交换都是以json格式传输的。而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用: DataContractJsonSerializer, JavaScriptSerializer 和 Json.NET 即Newtonsoft.Json。大多数人都会选择性能以及通用性较好Json.NET,这个不是微软的类库,但是一个开源的世界级的Json操作类库,从下面的性能对比就可以看到它的其中之一的性能优点。 齐全的 API 介绍,使用方式简单 回到顶部

C#中JSON字符串中的转义字符

白昼怎懂夜的黑 提交于 2020-08-04 23:29:03
新建一个.NET Core控制台项目,然后引入Json.NET的NuGet包: Newtonsoft.Json ,我们使用Json.NET将类序列化为JSON字符串,再将JSON字符串反序列化为类,代码如下: using Newtonsoft.Json; using System; namespace NetCoreJson { public class MessageContainer { public string Message1 { get ; set ; } public string Message2 { get ; set ; } } class Program { static void Main( string [] args) { MessageContainer messageContainerToJson = new MessageContainer() { Message1 = " 这是带双引号转义字符的消息:\"今天是个好天气!\" " , Message2 = " 这是带换行的消息:今天天气真好\n适合出去郊游\n真是愉快的一天 " }; string toJsonMessage = JsonConvert.SerializeObject(messageContainerToJson, Formatting.Indented); Console

C#JSON与XML相互转换

孤者浪人 提交于 2020-08-04 19:05:43
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using Newtonsoft.Json; namespace JSonConverter { class Program { static void Main(string[] args) { string xml = "<Test><Name>Test class</Name><X>100</X><Y>200</Y></Test>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc); Console.WriteLine("XML -> JSON: {0}", json); Console.ReadLine(); } } } json2xml  预定义的Json字符串如上   同理调用Json .Net 类库中的方法   XmlDocument doc1 = JsonConvert.DeserializeXmlNode(json);   Console.WriteLine(doc1