C
C#2y ago
RockyDf01

❔ Get Array From Cookies using C#

Hi Everyone, So I got a problem with getting the Cart array from cookies, I use this code
Request.Cookies.TryGetValue($"Cart{user.Id}", out var cart);
Request.Cookies.TryGetValue($"Cart{user.Id}", out var cart);
to get the array out of cookie but it returns me this "[8" and what I got stored in the Cookies its
Cart1=[8,2002,6,4]
Cart1=[8,2002,6,4]
3 Replies
Anchy
Anchy2y ago
I'm assuming when you say it returns you "[8", that value is from the debugger?
Angius
Angius2y ago
Cookies are separated with commas. Cookies are basically stored as
foo=bar,baz=34323,token=b84tbysdf8tv3,theme=dark
foo=bar,baz=34323,token=b84tbysdf8tv3,theme=dark
So cookies like
foo=bar,baz=[234,543,676],theme=light
foo=bar,baz=[234,543,676],theme=light
will be treated as
foo = bar
baz = [234
543 =
676] =
theme = light
foo = bar
baz = [234
543 =
676] =
theme = light
https://github.com/dotnet/runtime/issues/58773 HttpClient suffers a similar issue, apparently Seems like it was fixed with .NET 7, but the issue isn't closed yet so ¯\_(ツ)_/¯ I'd maybe encode the cookie value, treat it as a string, then decode it server-side
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.