GetUserInfoAsync & Newtonsoft.Json.JsonSerializationException

Hello,

I add a rule to add custom claims -claims form my ADFS server - into the Auth0 user profile. Into my dot net core WebAPI, I’m getting those custom claim by calling the AuthenticationApiClient.GetUserInfoAsync function.

The weird behavior is when I throw a new Exception afterward, I receive a
Newtonsoft.Json.JsonSerializationException Error getting value from 'Result' on 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[<WebAPI project>,System.Runtime.CompilerServices.IAsyncStateMachine]'.

Observations:

  • If I throw an Exception just before the GetUserInfoAsync call, it’s caught by the ExceptionHandler without a problem.
  • I tried with a “real” Exception (i.e. SecurityException) or a custom one (i.e. DummyException)
  • The Exception can contain a message error or not, the SerializationException is raise nonetheless

The only way around is that add the custom claim in the access_token but, preferably, I would like to avoid that scenario.

Anyone as seen this behavior previously?

Thanks

EDIT: The JsonSerializationException is raised when I try to get a FirstOrDefault on an IQueryable.

Hey there @smnbnt, are you seeing any other details in the logs when the error occurs? Any additional information we can get on the subject can help us in our pursuit. Thanks!

Hey @Jim.Morrison, thank for the reply.
The WebAPI project is running under .NET core 2.0 if that could of any help.

Here the content in the Output in Visual Studio:

Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in System.Private.CoreLib.dll
The thread 92 has exited with code 0 (0x0).
The thread 0x5c has exited with code 0 (0x0).
Loaded '/root/.nuget/packages/system.linq.dynamic.core/1.0.8.18/lib/netstandard2.0/System.Linq.Dynamic.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.0/System.IO.MemoryMappedFiles.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
[13:39:08 FTL] Newtonsoft.Json.JsonSerializationException: Error getting value from 'Result' on 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[<WEBAPI_PROJECT_FUNCTION>]'. ---> System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 94
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 96
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 53
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
   --- End of inner exception stack trace ---
   at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 47
Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
[13:39:10 ERR] An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Headers are read-only, response has already started.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
   at Microsoft.AspNetCore.Http.Internal.DefaultHttpResponse.set_ContentType(String value)
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 111
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 51
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.UsePathBaseMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
[13:39:10 WRN] The response has already started, the error page middleware will not be executed.
[13:39:10 ERR] Connection id "0HLJ4TO2AT8DS", Request id "0HLJ4TO2AT8DS:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: Headers are read-only, response has already started.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
   at Microsoft.AspNetCore.Http.Internal.DefaultHttpResponse.set_ContentType(String value)
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 111
   at <WEBAPI_PROJECT_FUNCTION> in <WEBAPI_PROJECT_FUNCTION>:line 51
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.UsePathBaseMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)
   at SimpleInjector.Integration.AspNetCore.RequestScopingStartupFilter.<ConfigureRequestScoping>b__3_0(HttpContext context, Func`1 next)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

Otherwise, if I could have access to the content of idToken without calling GetAdfsClaimsAsync() (which seem to be at the heart of the problem) it would be a workaround.

Never mind, the problem is deeper in my code, without using any Auth0 functions and packages, I got the same JsonSerilizationException.

Ah in some initial research I found this Microsoft write-up on JsonSerilizationException below. While I’m not sure on whether or not it will help you in your quest, it may help to start. Please keep us posted on if you need any additional help when it comes to implementing Auth0 into your app.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.