Skip to content

Commit 0a7997d

Browse files
committed
Introduce CustomJwtPayload to fix 'cnf' serialization issue
1 parent 9b2302e commit 0a7997d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IdentityModel.Tokens.Jwt;
5+
using System.Security.Claims;
6+
7+
namespace IdentityServer4.Extensions
8+
{
9+
internal class CustomJwtPayload : JwtPayload
10+
{
11+
public CustomJwtPayload(string issuer, string audience, IEnumerable<Claim> claims, DateTime? notBefore, DateTime? expires) :
12+
base(issuer, audience, claims, notBefore, expires)
13+
{
14+
}
15+
16+
public override string SerializeToJson()
17+
{
18+
return JsonConvert.SerializeObject(this);
19+
}
20+
}
21+
}

src/IdentityServer4/src/Extensions/TokenExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static class TokenExtensions
3333
/// </exception>
3434
public static JwtPayload CreateJwtPayload(this Token token, ISystemClock clock, IdentityServerOptions options, ILogger logger)
3535
{
36-
var payload = new JwtPayload(
36+
var payload = new CustomJwtPayload(
3737
token.Issuer,
3838
null,
3939
null,
@@ -48,7 +48,7 @@ public static JwtPayload CreateJwtPayload(this Token token, ISystemClock clock,
4848
var amrClaims = token.Claims.Where(x => x.Type == JwtClaimTypes.AuthenticationMethod).ToArray();
4949
var scopeClaims = token.Claims.Where(x => x.Type == JwtClaimTypes.Scope).ToArray();
5050
var jsonClaims = token.Claims.Where(x => x.ValueType == IdentityServerConstants.ClaimValueTypes.Json).ToList();
51-
51+
5252
// add confirmation claim if present (it's JSON valued)
5353
if (token.Confirmation.IsPresent())
5454
{
@@ -83,7 +83,7 @@ public static JwtPayload CreateJwtPayload(this Token token, ISystemClock clock,
8383
var amrValues = amrClaims.Select(x => x.Value).Distinct().ToArray();
8484
payload.Add(JwtClaimTypes.AuthenticationMethod, amrValues);
8585
}
86-
86+
8787
// deal with json types
8888
// calling ToArray() to trigger JSON parsing once and so later
8989
// collection identity comparisons work for the anonymous type
@@ -125,7 +125,7 @@ public static JwtPayload CreateJwtPayload(this Token token, ISystemClock clock,
125125
var newArr = new List<JToken>();
126126
foreach (var arrays in group)
127127
{
128-
var arr = (JArray)arrays.JsonValue;
128+
var arr = (JArray) arrays.JsonValue;
129129
newArr.AddRange(arr);
130130
}
131131

0 commit comments

Comments
 (0)