Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate cookies #2077

Open
rawensoft opened this issue May 4, 2023 · 4 comments
Open

Duplicate cookies #2077

rawensoft opened this issue May 4, 2023 · 4 comments
Labels

Comments

@rawensoft
Copy link

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.

@rawensoft rawensoft added the bug label May 4, 2023
@alexeyzimarev
Copy link
Member

Well, you should either use the container for RestClient or for the message handler. If you use RestClient cookies, it handles stuff correctly. If you also use a custom container for the message handler - you will get these weird side effects.

@rawensoft
Copy link
Author

At first I also used one container for everything, but this only duplicated the cookies (old + updated), so I decided to try with two containers and settled on this option, because. seemed like a better option.

@alexeyzimarev
Copy link
Member

I think you would make it work easier if you use a custom container for the message handler only.

I will try to check what causes the cookies to duplicate. Maybe we need a combined test for cookies and redirect.

@rassilon
Copy link

@alexeyzimarev , I added a couple of cookie redirect tests in #2119 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants