Azure-activedirectory-identitymodel-extensions-for-dotnet: JwtSecurityToken.EncodedPayload is having problems with my Deserialized JSON to plain object

Created on 23 Jun 2020  路  8Comments  路  Source: AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet

This is a "It used to work" issue. I have attached the code that shows the issue.
I want to be able to Deserialize a JSON into an object that I can add correctly to a JwtPayload and get properly encoded by the JwtSecurityToken.

I can add a object to the payload.

var custom = new Custom{}
payload.Add("custom", custom);

I can add a dynamic to the payload.

dynamic ccDynamic = new
            {
                a="b",
                b=2,
                id_token_signing_alg_values_supported = new List<string>{
                        "RS256"
                },
                d = new { 
                  name  = "bob"
                }
            };
payload.Add("ccDynamic", ccDynamic);

I can add an expando to the payload.

var converter = new ExpandoObjectConverter();
dynamic expando = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
payload.Add("expando", expando);

And these get encoded just fine in the final JWT.

I used to be able to deserialize a json to a dynamic or object and add that.

   var json = JsonConvert.SerializeObject(custom);
    var jObj = JsonConvert.DeserializeObject(json);
    dynamic jDynamic = JsonConvert.DeserializeObject<dynamic>(json);
    payload.Add("object", jObj);
    payload.Add("jDynamic", jDynamic);

However those Deserialized object now turn into the following.
JWT

{
  "nbf": 1592932718,
  "exp": 1592934518,
  "iat": 1592932718,
  "iss": "https://issuer.com/token",
  "aud": "my-audience",
  "object": {
    "Name": [],
    "Numbers": [
      [],
      [],
      []
    ],
    "Strings": [
      [],
      [],
      []
    ],
    "SomeObjects": [
      {
        "Name": []
      },
      {
        "Name": []
      }
    ],
    "SomeObject": {
      "Name": []
    }
  },
  "jDynamic": {
    "Name": [],
    "Numbers": [
      [],
      [],
      []
    ],
    "Strings": [
      [],
      [],
      []
    ],
    "SomeObjects": [
      {
        "Name": []
      },
      {
        "Name": []
      }
    ],
    "SomeObject": {
      "Name": []
    }
  },
  "ccDynamic": {
    "a": "b",
    "b": 2,
    "id_token_signing_alg_values_supported": [
      "RS256"
    ],
    "d": {
      "name": "bob"
    }
  },
  "custom": {
    "Name": "Bugs Bunny",
    "Numbers": [
      1,
      2,
      3
    ],
    "Strings": [
      "a",
      "bb",
      "ccc"
    ],
    "SomeObjects": [
      {
        "Name": "Daisy Duck"
      },
      {
        "Name": "Porky Pig"
      }
    ],
    "SomeObject": {
      "Name": "Daffy Duck"
    }
  },
  "expando": {
    "Name": "Bugs Bunny",
    "Numbers": [
      1,
      2,
      3
    ],
    "Strings": [
      "a",
      "bb",
      "ccc"
    ],
    "SomeObjects": [
      {
        "Name": "Daisy Duck"
      },
      {
        "Name": "Porky Pig"
      }
    ],
    "SomeObject": {
      "Name": "Daffy Duck"
    }
  }
}

CustomPayloadJwt.zip

Bug Customer reported P1

All 8 comments

btw: I first saw this in the following code.

var jsonValue = Newtonsoft.Json.Linq.JRaw.Parse(json);
payload.Add("jsonValue ", jsonValue );

The jsonValue here looks exactly like

 var jObj = JsonConvert.DeserializeObject(json);

the following in field code looks like it might be effected.
IdentityServer4 TokenExtension

Its doing a JRaw and puts that into the payload.

@ghstahl sorry for the hassle.
by "used to work" i assume you mean all the empty objects didn't exist.

the

"jDynamic": { currently bad}

looked like.

"expando": { all good here....}

Yes the payload should correctly parse that jDynamic and jObj object I pass in. I don't know if that happens in JwtPayload or JwtSecurityToken.

@ghstahl do you know a version that this was working in?

Works with this version

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />

If you take my attached code and change it to this its good to go.
I checked all the 6.x.x versions and they all exhibit the problem.
6.6.0,6.5.1,6.5.0

@ghstahl that helps thanks.
I think I have an idea now.

@ghstahl we have an internal version of Newtonsoft, i haven't found the exact issue, but I believe the two are out of sync.

To test this out, I added the following code and this seems to have things in sync and everything seems right.
You could use this workaround while we figure out what to do.

JsonExtensions.Serializer = JsonConvert.SerializeObject;
JsonExtensions.Deserializer = JsonConvert.DeserializeObject;

eyJhbGciOiJFUzI1NiIsImtpZCI6IjEyMzQiLCJ0eXAiOiJKV1QifQ.eyJuYmYiOjE1OTI5NTY5ODMsImV4cCI6MTU5Mjk1OD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x7Ik5hbWUiOiJQb3JreSBQaWcifV0sIlNvbWVPYmplY3QiOnsiTmFtZSI6IkRhZmZ5IER1Y2sifX19.BLAH

{
"nbf": 1592956983,
"exp": 1592958783,
"iat": 1592956983,
"iss": "https://issuer.com/token",
"aud": "my-audience",
"object": {
"Name": "Bugs Bunny",
"Numbers": [
1,
2,
3
],
"Strings": [
"a",
"bb",
"ccc"
],
"SomeObjects": [
{
"Name": "Daisy Duck"
},
{
"Name": "Porky Pig"
}
],
"SomeObject": {
"Name": "Daffy Duck"
}
},
"jDynamic": {
"Name": "Bugs Bunny",
"Numbers": [
1,
2,
3
],
"Strings": [
"a",
"bb",
"ccc"
],
"SomeObjects": [
{
"Name": "Daisy Duck"
},
{
"Name": "Porky Pig"
}
],
"SomeObject": {
"Name": "Daffy Duck"
}
},
"ccDynamic": {
"a": "b",
"b": 2,
"id_token_signing_alg_values_supported": [
"RS256"
],
"d": {
"name": "bob"
}
},
"custom": {
"Name": "Bugs Bunny",
"Numbers": [
1,
2,
3
],
"Strings": [
"a",
"bb",
"ccc"
],
"SomeObjects": [
{
"Name": "Daisy Duck"
},
{
"Name": "Porky Pig"
}
],
"SomeObject": {
"Name": "Daffy Duck"
}
},
"expando": {
"Name": "Bugs Bunny",
"Numbers": [
1,
2,
3
],
"Strings": [
"a",
"bb",
"ccc"
],
"SomeObjects": [
{
"Name": "Daisy Duck"
},
{
"Name": "Porky Pig"
}
],
"SomeObject": {
"Name": "Daffy Duck"
}
}
}

@ghstahl this seems to be a broader issue.
Closing this and linking to #1475

Was this page helpful?
0 / 5 - 0 ratings