33
44using Microsoft . Extensions . Caching . Distributed ;
55using Microsoft . Extensions . Logging ;
6- using Newtonsoft . Json ;
76using System ;
87using System . Collections . Generic ;
98using System . Linq ;
109using System . Security . Claims ;
1110using System . Text ;
11+ using System . Text . Json ;
1212using System . Threading . Tasks ;
1313
1414namespace IdentityModel . AspNetCore . OAuth2Introspection
1515{
1616 internal static class CacheExtensions
1717 {
18- internal readonly static JsonSerializerSettings Settings ;
18+ internal readonly static JsonSerializerOptions Options ;
1919
2020 static CacheExtensions ( )
2121 {
22- Settings = new JsonSerializerSettings ( ) ;
23- Settings . Converters . Add ( new ClaimConverter ( ) ) ;
22+ Options = new JsonSerializerOptions
23+ {
24+ IgnoreReadOnlyFields = true ,
25+ IgnoreReadOnlyProperties = true ,
26+ IgnoreNullValues = true
27+ } ;
28+
29+ Options . Converters . Add ( new ClaimConverter ( ) ) ;
2430 }
2531
2632 public static async Task < IEnumerable < Claim > > GetClaimsAsync ( this IDistributedCache cache , string cacheKeyPrefix , string token )
@@ -33,7 +39,7 @@ public static async Task<IEnumerable<Claim>> GetClaimsAsync(this IDistributedCac
3339 }
3440
3541 var json = Encoding . UTF8 . GetString ( bytes ) ;
36- return JsonConvert . DeserializeObject < IEnumerable < Claim > > ( json , Settings ) ;
42+ return JsonSerializer . Deserialize < IEnumerable < Claim > > ( json , Options ) ;
3743 }
3844
3945 public static async Task SetClaimsAsync ( this IDistributedCache cache , string cacheKeyPrefix , string token , IEnumerable < Claim > claims , TimeSpan duration , ILogger logger )
@@ -66,7 +72,7 @@ public static async Task SetClaimsAsync(this IDistributedCache cache, string cac
6672 absoluteLifetime = now . Add ( duration ) ;
6773 }
6874
69- var json = JsonConvert . SerializeObject ( claims , Settings ) ;
75+ var json = JsonSerializer . Serialize ( claims , Options ) ;
7076 var bytes = Encoding . UTF8 . GetBytes ( json ) ;
7177
7278 logger . LogDebug ( "Setting cache item expiration to {expiration}" , absoluteLifetime ) ;
0 commit comments