question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with CookieContainer when use HttpClientFactory

See original GitHub issue

Describe the bug

HttpClient objects which are created from HttpClientFactory may use the same pooled HttpClientHandler object, so they may use the same CookieContainer object. This causes a thread-safe issue.

To Reproduce

Assume we have a web application call to api/Token (GET) of API server.

Web:

       public async Task<string> GetToken()

        {
            var httpClient = _clientFactory.CreateClient();
            var response = await httpClient.GetAsync("http://api.local:5089/api/token");
            var token = await response.Content.ReadAsStringAsync();
            return token;
        }

API:

        public ActionResult<string> Get()
        {   
            string token = HttpContext.Request.Cookies.ContainsKey("Token") ? 
                           HttpContext.Request.Cookies["Token"] : Guid.NewGuid().ToString();
            
            HttpContext.Response.Cookies.Append("Token", token);
            return token;
        }

Step 1: Make the first request to API, in Web application, response.Headers.GetValues("Set-Cookies") will show “Token” cookie’s value equals “abc” Step 2: Make a second request to API, in Web application, response.Headers.GetValues("Set-Cookies") will show “Token” cookie’s value also equals “abc”

Expected behavior

HttpClient objects which are created by HttpClientFactory should have the same behavior as creating new HttpClient

If we use new HttpClient(), each request to API, we will get new Token value, but we cannot achieve this purpose with HttpClientFatory. How could we deal with this problem when we have to use Cookie in API?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
manigandhamcommented, Nov 25, 2019

You can configure the underlying HttpMessageHandler to not use a cookie container, which means you have to handle the cookies yourself from the headers.

services.AddHttpClient("default")
    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
    {
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, 
        AllowAutoRedirect = true,
        UseCookies = false // <- set this as false to stop automatic cookie handling
    });
0reactions
rynowakcommented, Nov 24, 2019

Closing this, I’ve created an issue and PR on the docs repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with CookieContainer when use HttpClientFactory
Describe the bug HttpClient objects which are created from HttpClientFactory may use the same pooled HttpClientHandler object, ...
Read more >
Do pooled HttpClient instances keep the CookieContainer?
The pooled HttpMessageHandler instances results in CookieContainer objects being shared. Unanticipated CookieContainer object sharing often ...
Read more >
Make HTTP requests using IHttpClientFactory in ASP.NET ...
Unanticipated CookieContainer object sharing often results in incorrect code. For apps that require cookies, consider either: Disabling ...
Read more >
IHttpClientFactory can share cookies? What's the best ...
The pooled HttpMessageHandler instances results in CookieContainer objects being shared. Unanticipated CookieContainer object sharing often ...
Read more >
Use IHttpClientFactory to implement resilient HTTP requests
Issues with the original HttpClient class available in .NET; Benefits of using IHttpClientFactory; Multiple ways to use IHttpClientFactory ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found