This is what you need to get a list of cookies;
private async Task<List<Cookie>> GetCookies(string url, string cookieName) { var cookieContainer = new CookieContainer(); var uri = new Uri(url); using (var httpClientHandler = new HttpClientHandler { CookieContainer = cookieContainer }) { using (var httpClient = new HttpClient(httpClientHandler)) { await httpClient.GetAsync(uri); List<Cookie> cookies = cookieContainer.GetCookies(uri).Cast<Cookie>().ToList(); return cookies; } } }
and if you need only one cookie value here's how
private async Task<string> GetCookieValue(string url) { var cookieContainer = new CookieContainer(); var uri = new Uri(url); using (var httpClientHandler = new HttpClientHandler { CookieContainer = cookieContainer }) { using (var httpClient = new HttpClient(httpClientHandler)) { await httpClient.GetAsync(uri); var cookie = cookieContainer.GetCookies(uri).Cast<Cookie>().FirstOrDefault(x => x.Name == cookieName); return cookie?.Value; } } }
Result. Useawait authClient.PostAsJsonAsync()instead.