23

I'm trying to create an OAuth Handler in .Net Core, The api I'm using wraps the user data response in an property called data, But the OAuthTicket Context Expects a JObject and Not A JToken

var payload = JObject.Parse(await response.Content.ReadAsStringAsync())["data"];        
var context = new OAuthCreatingTicketContext(new ClaimsPrincipal(identity),
                    properties, Context, Scheme, Options, Backchannel, tokens, payload);

context.RunClaimActions();

How can I Convert a JToken To JObject?

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 19
    `JObject` is a subclass of `JToken`, so if `payload` is in fact a `JObject`, you can just cast it. See [JSON.NET: Why Use JToken--ever?](https://stackoverflow.com/q/38211719/3744182). But we need to see the JSON to be sure that will work. – dbc Nov 03 '18 at 05:10
  • @dbc Isn't the inheritance chain go as the following? JObject -> JContainer -> JToken. It's a bit wierd to me casting a base class to its super – johnny 5 Nov 03 '18 at 05:14
  • Hmm, It seems JContainer and JToken are abstract. Thanks – johnny 5 Nov 03 '18 at 05:16
  • Close as a duplicate of [JSON.NET: Why Use JToken--ever?](https://stackoverflow.com/q/38211719/3744182) then, or add an answer? – dbc Nov 03 '18 at 17:15
  • @dbc that link answers the question pretty much, the questions a bit different but I don’t want the split the votes let’s close as duplicate, so if anyone gets here it will link to your answer there – johnny 5 Nov 03 '18 at 17:21
  • OK, done. Glad to help. – dbc Nov 03 '18 at 17:22
  • 7
    try this `JObject object = token.ToObject();` – Alanight Jul 01 '19 at 09:07

0 Answers0