I\'ve got the following code that works successfully. I can\'t figure out how to get the cookie out of the response. My goal is that I want to be able to set cookies in th
There's alternative if you don't have access to the HttpClient
and can't inject the CookieContainer
. This works in .NET Core 2.2:
private string GetCookie(HttpResponseMessage message)
{
message.Headers.TryGetValues("Set-Cookie", out var setCookie);
var setCookieString = setCookie.Single();
var cookieTokens = setCookieString.Split(';');
var firstCookie = cookieTokens.FirstOrDefault();
var keyValueTokens = firstCookie.Split('=');
var valueString = keyValueTokens[1];
var cookieValue = HttpUtility.UrlDecode(valueString);
return cookieValue;
}