0

I'm using a central API in ASP.Net Core to authenticate users and to create JSON Web Tokens. The central API, in turn, calls other APIs to get other data, such as user profile data and subscription information. However, the central API receives double-serialized JSON objects that I cannot seem to deserialize. Here's the code from the central API:

Request.Host = new HostString(_usersApi);

HttpRequestMessageFeature hreqmf = new HttpRequestMessageFeature(HttpContext);
HttpRequestMessage httpRequestMessage = hreqmf.HttpRequestMessage;

var client = new HttpClient();
var response = await client.SendAsync(httpRequestMessage, 
HttpCompletionOption.ResponseHeadersRead);
var data = response.Content.ReadAsStringAsync();

var deserialized = JsonConvert.DeserializeObject<MyObject>(data.Result);

return Ok(deserialized);

data.Result equals "{\"property1\":\"value1\",\"property2\":\"value2\"}"

I cannot deserialize it to

{"Property1": "value1", "Property2": "value2"}

What am I missing? Thanks.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
IMOsiris
  • 111
  • 1
  • 2
  • 11
  • 1
    Why do you mix blocking (`data.Result`) and non-blocking (`await ... SendAsync()`) async calls? – Peter Csala Nov 22 '21 at 14:58
  • 1
    Could you please share wit us your `MyObject` class definition? – Peter Csala Nov 22 '21 at 15:00
  • 2
    Is that actually the value of the result, or is that just what Visual Studio is showing you when you debug? Strings in VS debug windows will be shown including C# escape characters by default, even though those characters aren't actually part of the string. – Richard Deeming Nov 22 '21 at 16:14
  • @PeterCsala As for mixing blocking and non-blocking calls, I did it merely for convenience. Do you advise against that? – IMOsiris Nov 23 '21 at 21:26
  • @IMOsiris Always prefer await over .Result or .GetAwaiter().GetResult() – Peter Csala Nov 23 '21 at 21:29
  • @PeterCsala MyObject is actually based on the class Google.Authenticator.SetupCode. Instead of using that class directly, I had to create a proxy with the properties, Account, ManualEntryKey, and QrCodeSetupImageUrl. Then everything worked properly. – IMOsiris Nov 23 '21 at 21:30
  • If you have solved your problem then please leave a post and mark it as the answer. – Peter Csala Nov 24 '21 at 07:31

0 Answers0