CookieManager icon indicating copy to clipboard operation
CookieManager copied to clipboard

Newtonsoft.Json.JsonReaderException

Open sylvainb31 opened this issue 5 years ago • 4 comments

Hello,

When getting a cookie containing a complex object, an exception is thrown : Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: Q. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at CookieManager.DefaultCookieManager.GetExisting[T](String key) at SecretariatMedical.Controllers.LoginController.Index(LoginViewModel model, Nullable1 OuvreNouvelleConnexion) in C:\Users\SylvainB\Desktop\aspnetcore\Projet Clinique Charles\SecretariatMedical\SecretariatMedical\Controllers\LoginController.cs:line 164 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 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.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)`

Here is the line where the problem happens :

Dictionary<string, Dictionary<string, ClientSiteIdentifier>> alreadyExistingList = _cookieManager.Get<Dictionary<string, Dictionary<string, ClientSiteIdentifier>>>("ListeConnexionsMultiSiteParUtilisateur");

Here is the line where I store the object in the cookie :

_cookieManager.Set("ListeConnexionsMultiSiteParUtilisateur", alreadyExistingList, new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(30) });

Do you have any idea what could cause this problem ?

Thanks for your help !

Sylvain

sylvainb31 avatar Nov 22 '19 09:11 sylvainb31

It also happens on a string stored :

var tokenInCookie = _cookieManager.Get<string>("token_auth");

_cookieManager.Set("token_auth", tokenWithBearer, cookieOptionsToken);

Here is what the value of the cookie looks like :

Q2ZESjhJLXdFWlk4TUJ4QWlhWUlxT0Rub3JUTXVPS0NOY0JJUEtIX2ZaTTI4ampTSHdJRVhZVGlOeDNTZzhtZEI4LWhPblRhNEEzTVNra0ZhT3ZlWGU4UTdQLUdtUEZyV0g0Ykgxb0plczIwWFNzWWlQeV8wYUxSWmI5c3l1UkxpWmVGV28yTXhJek9GaDRuUTlXaE5MVmNoRXJ0aVZxN1FvT3UzWGVRbE9UVkUzMTlxV1V5b21KUkRpU25sN0dvZkhhNXBONkNnZU1GMngxSTZDUnFVRU9YYkdFZm10OXBFYzJKRS1uSk9jZU0yUGhMTDVrdXY1bEZKOHppNmxULXJGaGhjTHVrcU1EN2szaWU3eEE3alB0VXdrY2M5MmFaUVNEb3Jma0R6cFZMTUdLR3B5V1o1ZWt3VEJYTzU3bzZVR3dkdVItUUpPMFVyekZZeGdpS2x3cFBxTTdJYkRXdktmU055YUJ5bGc5dVZUY1BTYldxOXRyMFR2cVVFVWVadE51UDBGX0JMVE5FWWNzN0drVFFnaXJWaG1VcjBpZm1Ba3RWM0FGZ1VmT0wxTUpSS3dmWERiWmRGenRyOG01eUpyTWYzUVc0NGhYc3ZYX1BNMjBDb25LU0NBcGNrY1J4SDYycTBsdjc1MTQtT3VOTkxKc1J1empyOHVWSEttU3pBR1RROXZ5ZVp6U2UyZGJ0OVpWR1FKTkxtYXlnOTk2WFN3MnRhMTZxd2M0S3VwSkc5WkdRSVlSSmI0TkhqV0daSVdaV183Z3k5X2tJQTcwNUlTVnFOSXR5X2I4SGUyUkFvbUVhNnJWM2V3aVJoaGNMdmd1Z3B6THdDdlFfNEIyR2NITW5WWHRaZkJyRXdaaUJDdldFT3h4TXlQMzFxZnR4d0R2WjNxeUprSWdtZGd6YXQxamhNeVlwb05jbXJzZnA3c0pPX1E%3D

Once I clear the cookie in the web browser and launch the application again, there is no problem. But once I restart the server this happens again (it happens when my app is deployed).

sylvainb31 avatar Nov 22 '19 10:11 sylvainb31

try to change this:

_cookieManager.Set("ListeConnexionsMultiSiteParUtilisateur", alreadyExistingList, new CookieOptions() { HttpOnly = true, Expires = DateTime.Now.AddDays(30) });

with this: _cookieManager.Set("ListeConnexionsMultiSiteParUtilisateur", alreadyExistingList, new CookieOptions() { HttpOnly = true, Expires = DateTimeOffset.Now.AddDays(30) });

maxacampora avatar Jan 28 '20 15:01 maxacampora

I also had this error! I also solved it by clearing my cache. Hope this error doesn't occur for my clients.

UsefulJeroen avatar May 18 '20 15:05 UsefulJeroen

I'm getting the above, and was wondering if it will happen once the app is "out in the wild" or if it's just because my app is still in development therefore lots of changes happening?

adamdidwell avatar Feb 15 '22 16:02 adamdidwell