0

I am working with publicly available site https://studia3.elka.pw.edu.pl using Flurl.Http. The _restClient is obtained from PerBaseUrlFlurlClientFactory with address https://studia3.elka.pw.edu.pl/pl/ With certain request it should send me back a cookie for further use. I receive the cookie in the response (it is in the headers section) but the cookie is not present in my client's cookie dictionary (where I think it should appear after getting a response)

My code for making the request and reading the response is following:

 private const string StudiaIdCookieName = "STUDIA_SID";


var cook = new Cookie(name: "STUDIA_COOKIES", value: "YES&", path: "/", domain: ".studia3.elka.pw.edu.pl");

        var unauthenticatedCookiesRequest = _restClient.Request().WithCookie(cook);

        var unauthenticatedCookiesResponse = await unauthenticatedCookiesRequest.SendAsync(verb: HttpMethod.Get);

        if (!_restClient.Cookies.ContainsKey(key: StudiaIdCookieName))
            throw new Exception(message: "Failed to get unauthenticated cookie from Studia");

        var authenticateCookieRequest = _restClient.Request().AppendPathSegment(segment: LdapPathSegment);

        var authenticateCookieResponse = authenticateCookieRequest.PostUrlEncodedAsync(data:
            new { studia_login = username, studia_passwd = password });

The problem is with

    if (!_restClient.Cookies.ContainsKey(key: StudiaIdCookieName))
        throw new Exception(message: "Failed to get unauthenticated cookie from Studia");

as _restClient.Cookies only contains the cookie i manually set up and not the one from the response (the response cookie is called STUDIA_SID and it is present under headers section when i browse the response in debugger).

My question is: should the Cookies property reflect the changes of making request? If not, should I manually check the headers and extract cookie from there?

MikelThief
  • 185
  • 1
  • 13
  • Response cookies should be present in the `Cookies` collection. When you say you are seeing it in the response header, and can you provide the details on that? Mask any sensitive data obviously but be as specific as possible with the header name and value. – Todd Menier May 08 '19 at 15:43
  • @ToddMenier please see the attached screenshot: https://imgur.com/a/5nQ8cpa I didn't mask anything as the data I described here can be reached by anyone :) – MikelThief May 08 '19 at 19:18
  • Ok, just wanted to rule out that it's not a malformed cookie header or something. Looks right. Is this a multithreaded app? Any chance `_restClient` is being used concurrently in multiple places? – Todd Menier May 08 '19 at 19:51
  • @ToddMenier `_restclient` is used only in this single place. There is no concurrency. Those cookies in fiddler look strange though. https://imgur.com/a/Z8MRZqW See "&" at the end. Maybe it is a dotnet problem to parse them and not library problem? – MikelThief May 08 '19 at 19:54
  • doing anything with Flurl's config/factory stuff to mess with underlying HttpClient or HttpMessageHandler? I'm guessing not. Try simplifying it in 2 ways: 1) just create the FlurlClient instead of getting it from a factory, and 2) use `WithCookie(name, value)` instead of creating the `Cookie` yourself. Basically, get to look more like [this](https://stackoverflow.com/a/47292309/62600), which is the most common pattern. (I'll admit I'm kind of stumped so far.) – Todd Menier May 08 '19 at 20:06
  • @ToddMenier I changed `_restclient` to be created manually by me with `new` keyword and added cookie via `WithCookie(name, value)`. No effect. I also don't mess with any classes inside Flurlclient. – MikelThief May 08 '19 at 21:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193058/discussion-between-mikelthief-and-todd-menier). – MikelThief May 08 '19 at 22:29

0 Answers0