Skip to content

Duplicate cookies #2077

Open
Open
@rawensoft

Description

@rawensoft

Describe the bug
The problem is duplication of cookies, which causes an endless Redirect. There are two CookieContainer. One is added to the RestClient.CookieContainer, along with the main cookies, and the second stores the cookies received when processing the Redirect or after the request is completed, according to this decision 2045#issuecomment-1500095598. The problem is that the first and second CookieContainer are added in turn to the request, which causes duplication.

        var containerRequest = new CookieContainer();
        var containerResponse = new CookieContainer();
        var client = new RestClient(new RestClientOptions() 
        {
            AutomaticDecompression = DecompressionMethods.All,
            FollowRedirects = true,
            MaxRedirects = 10,
            UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
            BaseUrl = new Uri(url),
            AllowMultipleDefaultParametersWithSameName = true,
            RemoteCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; },
            CookieContainer = containerRequest,
            ConfigureMessageHandler = h =>
            {
                ((HttpClientHandler)h).UseCookies = true;
                ((HttpClientHandler)h).CookieContainer = containerResponse;
                return h;
            }
        });

image

Desktop (please complete the following information):

  • OS: Windows 10 Pro 22H2 19045.2846
  • .NET version .NET 6.0.16
  • Version 110.2.0

Additional context
At first I had one CookieContainer in RestClient and HttpClientHandler, but one of the users complained that he was always getting errors. When browsing through the http debugger, I found out that the cookies were duplicated, i.e. in the beginning there were cookies that were before the request, and then there were updated cookies, although the CookieContainer contained updated cookies.
image
Then I thought to put cookies only in HttpClientHandler, but with Redirect they were reset and it turned out that I was not authorized, if I put cookies only in RestClient.CookieContainer, then I will not receive cookies from the request and they will not be updated with Redirect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions