1

I have this function:

 [HttpPost] [Route("api/authenticate")] public async Task<HttpResponseMessage> Authenticate(Auth a) { var cookies = Request.Headers.GetCookies() } 

After seeing a lot of posts on StackOverflow, I saw people trying to do this:

CookieHeaderValue cookie = Request.Headers.GetCookies("session-id").FirstOrDefault(); 

But I don't see the FirstOrDefault method. When I run GetCookies, I got a string array with one element, and inside of it:

"{cookieKey=cookieValue; cookie2=cookie2}" 

Should I convert this string to dictionary, or am I missing something on the API?

2 Answers 2

2

Quite hack, but works

using System.Linq; var cookies = Request.Headers .GetCookies()[0].Cookies .ToDictionary(cookie => cookie.Key, cookie=> cookie.Value); return cookies["cookie-index"]; 
Sign up to request clarification or add additional context in comments.

1 Comment

How the hell the API does not have anything for this?
-1

https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcontext.current?view=netframework-4.8

HttpContext.Current.Request.Cookies.Get(CookieName); 

1 Comment

This isn't System.Net, though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.