Skip to content

HttpClient not sending cookies which were part of earlier redirect response in Core Clr #15921

Closed
dotnet/corefx
#5001

Description

@kichalla

I am using CoreClr version of HttpClient on my Windows 10 OS.

Scenario: Successful login will redirect the user to a page where the logging in user info is displayed

Issue: Since the necessary cookie is not present, the system thinks that the user is not logged in

Trace of calls for the above scenario(Note: all these messages were logged at the server side)


Request: request for login page
GET /account/login ||Connection: Keep-Alive||Accept-Encoding: gzip, deflate||Cookie: U58hr9PQBAQ=CfDJ8IcTvLBrmYtHm19jNmsLC62rbm_jC2LBGRmurk0fd2R9YeGdeOLdRpC-rEVSSK24hsMjOToMUnjgo_cZwFLrDge_a0sHVy5eJc9qYkjYvThiFcG7MA9MB-HYLsoxlQIzT6v8tmAUbTOZ-39AakEWLTI; .AspNet.Session=7e8cee49-8d6b-6ab5-357b-d29e964af60d||Host: localhost:5000

Response: login page sent to client
200 GET /account/login ||Date: Sat, 12 Dec 2015 14:27:39 GMT||Transfer-Encoding: chunked||Content-Type: text/html; charset=utf-8||Server: Kestrel||

Request: client request to login with details of username and password
POST /Account/Login ||Connection: Keep-Alive||Content-Length: 69||Content-Type: application/x-www-form-urlencoded||Accept-Encoding: gzip, deflate||Cookie: U58hr9PQBAQ=CfDJ8IcTvLBrmYtHm19jNmsLC62rbm_jC2LBGRmurk0fd2R9YeGdeOLdRpC-rEVSSK24hsMjOToMUnjgo_cZwFLrDge_a0sHVy5eJc9qYkjYvThiFcG7MA9MB-HYLsoxlQIzT6v8tmAUbTOZ-39AakEWLTI; .AspNet.Session=7e8cee49-8d6b-6ab5-357b-d29e964af60d||Host: localhost:5000||

Response: server successful login response with redirect to home page /
302 POST /Account/Login ||Cache-Control: no-cache||Date: Sat, 12 Dec 2015 14:27:39 GMT||Pragma: no-cache||Expires: -1||Location: /||Server: Kestrel||Set-Cookie: .AspNet.Microsoft.AspNet.Identity.Application=CfDJ8IcTvLBrmYtHm19jNmsLC60z9IhWh_D65HPs01cDjGLzpfs5B5SeoX4k5tem5BqxDavdDWtijmOSUKuWw_lkI3PEeWJjUHn_mk8ArChjrFgySrEuCSkQMZU4vGVL3R3F2_aNbpixFkI3Oh2_PN1VF9Peb1XUJJTZDCDLlUFP7QSreFC4qaiJWaWTpGdp-_nStQSr2OzP5qAvqbGn2ME4fICTfm7BqWas_h_WaZPWjOwRT68V9ewibMxR2odTbcQf3Bqie1IEx2kjwiYLdXrzaT2sc-TmzKnNSTELgWmCVBdV27dMM2YGlwUOIL_YTgyAqCNLSO_YjfG8_gTODvhjTCJ9ff8hc_xTx0PYiZMdah5oNZPmAyAC2FJh_hWCRQD1jzgDVVGp2le9TaEw6XPdvsa_2u1HbjH_vRBIdtd49i9ZJv9Z_rO5X3PDgTYkAahAlGx7ym_vDOTZA-VC3KvM-JMOyIfcjYjovU9QkhsXBg8UI5COArVxLPXHSyXADoltftMxOg8oKBdl-55I8bKe-0ep6haIAz6ozlSFR41fEQN_j9kyNCaSjZYGu6-A6Qu8YP6tGEb3EPGhicE15NtrkDj7YhnJruOGKip2ZMvv-V0xg1UfzmNgOvvpMZHElbFsL2jlPaP0nzPn4uffArK6hlxo_ExYOdp596omQjPCF4h-; path=/; httponly||

NOTE: This call is missing the .AspNet.Microsoft.AspNet.Identity.Application cookie

Request: client request to home page
GET / ||Connection: Keep-Alive||Accept-Encoding: gzip, deflate||Cookie: U58hr9PQBAQ=CfDJ8IcTvLBrmYtHm19jNmsLC62rbm_jC2LBGRmurk0fd2R9YeGdeOLdRpC-rEVSSK24hsMjOToMUnjgo_cZwFLrDge_a0sHVy5eJc9qYkjYvThiFcG7MA9MB-HYLsoxlQIzT6v8tmAUbTOZ-39AakEWLTI; .AspNet.Session=7e8cee49-8d6b-6ab5-357b-d29e964af60d||Host: localhost:5000||

Response:
200 GET / ||Date: Sat, 12 Dec 2015 14:27:39 GMT||Transfer-Encoding: chunked||Content-Type: text/html; charset=utf-8||Server: Kestrel||


NOTE that this scenario works fine with regular desktop HttpClient.

@Tratcher

Activity

  1. self-assigned this
    on Dec 12, 2015
  2. davidsh commented on Dec 12, 2015

    @davidsh
    Contributor

    Please post the source code to how you are using HttpClient. Need to see the specifics of whether you are using HttpClientHandler directly or WinHttpHandler. What are the handler properties you are setting regarding UseCookies, 'CookieContainer` etc.

    So, posting a small Visual Studio solution for this repro will assist in root causing the problem.

  3. kichalla commented on Dec 12, 2015

    @kichalla
    Author

    I'm just newing up the HttpClientHandler and not setting any properties on it explicitly...sure will send out a repro.

  4. kichalla commented on Dec 14, 2015

    @kichalla
    Author

    I have created a repro here(the readme has some details regarding its usage): https://github.com/kichalla/HttpClientCoreClrRepro

  5. davidsh commented on Dec 14, 2015

    @davidsh
    Contributor

    Thx for posting the repro. I'll take a look.

    However, I don't quite understand the entire flow. For example, where do the original 2 cookies (that you list in the first HTTP request) come from? Are there other HTTP request/responses that you didn't list above in the Fiddler trace?

    Cookie: U58hr9PQBAQ=CfDJ8IcTvLBrmYtHm19jNmsLC62rbm_jC2LBGRmurk0fd2R9YeGdeOLdRpC-rEVSSK24hsMjOToMUnjgo_cZwFLrDge_a0sHVy5eJc9qYkjYvThiFcG7MA9MB-HYLsoxlQIzT6v8tmAUbTOZ-39AakEWLTI;
    .AspNet.Session=7e8cee49-8d6b-6ab5-357b-d29e964af60d

    These cookies are listed in the HTTP request to be sent to the server. But I don't see any HTTP responses from the server that first did a 'Set-Cookie' on them?

  6. kichalla commented on Dec 14, 2015

    @kichalla
    Author

    Are there other HTTP request/responses that you didn't list above in the Fiddler trace?

    Yeah right, there are from prior requests. Yeah this is probably confusing, so my repro has a very simple repro of this issue without these details.

  7. davidsh commented on Dec 14, 2015

    @davidsh
    Contributor

    I've found the bug. It actually affects more cases than just redirect. It appears we drop response cookies in certain cases where WinHttpHandler is using CookieUsePolicy as CookieUsePolicy.UseSpecifiedCookieContainer (which is what the default HttpClientHandler does). In particular, we lose the Set-Cookie for 401 (server auth), 407 (proxy auth) and 3xx (redirect) responses.

    If you were using WinHttpHandler directly, it would, by default, use the internal-only (no CookieContainer) WinHTTP storage for cookies. And you wouldn't see this bug. So, you do see this bug in default cases where HttpClientHandler is used.

    I'll work on fix hopefully this week.

    cc: @CIPop @SidharthNabar

  8. davidsh commented on Dec 17, 2015

    @davidsh
    Contributor

    Reopen issue since the problem still happens on *NIX.

  9. davidsh commented on Dec 17, 2015

    @davidsh
    Contributor

    cc: @vijaykota @stephentoub

    I fixed this on Windows (WinHttpHandler) with PR dotnet/corefx#5001. But the problem is also in CurlHandler. Please see new tests added as well as design for fix by looking at PR dotnet/corefx#5001.

  10. removed their assignment
    on Dec 17, 2015
  11. vijaykota commented on Dec 18, 2015

    @vijaykota
    Contributor

    Thanks, @davidsh . @kapilash, it does seem like in FinishRequest, though we are doing AddResponseCookies for redirect case, it is being done with the request message Uri. Please try the repro on Unix

  12. stephentoub commented on Jan 4, 2016

    @stephentoub
    Member
  13. dsbenghe commented on Jul 8, 2016

    @dsbenghe

    The issue is still happening on Linux in some particular case: a back and forward handshake between 2 servers (different domains) with redirects between them. It works fine on Windows.

    What I see happening: if automatic redirects is on, and web site A redirects to website B - httpclient sends to site B the same cookies it sent to site A, but not the cookie set by site B previously. The CookieContainer seems to contains all the cookies for A and B, but the cookie is not sent to B.

    I can go in more details if needed and I have a fiddler trace too.

  14. stephentoub commented on Jul 8, 2016

    @stephentoub
    Member

    Can you share a repro? Which build are you using?

  15. dsbenghe commented on Jul 8, 2016

    @dsbenghe

    I'm using the rtm.
    I can share a repo privately.

  16. xantari commented on Jan 15, 2018

    @xantari

    Same issue with .NET 4.7. This only fixed in .NET Core?

  17. davidsh commented on Jan 15, 2018

    @davidsh
    Contributor

    Same issue with .NET 4.7. This only fixed in .NET Core?

    The bug in .NET Core was specific to .NET Core implementations.

    We have not received any bug reports regarding cookies being dropped in redirect requests when using .NET Framework. If you are having this problem, please open a new bug here:

    https://developercommunity.visualstudio.com/content/problem/post.html?space=61

    and please attach a simple repro so that it can be diagnosed. Thx.

  18. transferred this issue fromdotnet/corefxon Jan 31, 2020
  19. added this to the 1.0.0-rc2 milestone on Jan 31, 2020
  20. ghost locked as resolved and limited conversation to collaborators on Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Netbugos-linux

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions