Repository navigation
Description
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.
Activity
davidsh commentedon Dec 12, 2015
Please post the source code to how you are using HttpClient. Need to see the specifics of whether you are using
HttpClientHandlerdirectly orWinHttpHandler. What are the handler properties you are setting regardingUseCookies, 'CookieContainer` etc.So, posting a small Visual Studio solution for this repro will assist in root causing the problem.
kichalla commentedon Dec 12, 2015
I'm just newing up the HttpClientHandler and not setting any properties on it explicitly...sure will send out a repro.
kichalla commentedon Dec 14, 2015
I have created a repro here(the readme has some details regarding its usage): https://github.com/kichalla/HttpClientCoreClrRepro
davidsh commentedon Dec 14, 2015
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?
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?
kichalla commentedon Dec 14, 2015
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.
davidsh commentedon Dec 14, 2015
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
CookieUsePolicyasCookieUsePolicy.UseSpecifiedCookieContainer(which is what the default HttpClientHandler does). In particular, we lose theSet-Cookiefor 401 (server auth), 407 (proxy auth) and 3xx (redirect) responses.If you were using
WinHttpHandlerdirectly, 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
davidsh commentedon Dec 17, 2015
Reopen issue since the problem still happens on *NIX.
davidsh commentedon Dec 17, 2015
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.
vijaykota commentedon Dec 18, 2015
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
stephentoub commentedon Jan 4, 2016
Fixed by dotnet/corefx#5080.
dsbenghe commentedon Jul 8, 2016
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.
stephentoub commentedon Jul 8, 2016
Can you share a repro? Which build are you using?
dsbenghe commentedon Jul 8, 2016
I'm using the rtm.
I can share a repo privately.
xantari commentedon Jan 15, 2018
Same issue with .NET 4.7. This only fixed in .NET Core?
davidsh commentedon Jan 15, 2018
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.