simple-odata-client / Simple.OData.Client

MIT License
331 stars 197 forks source link

.NET Framework unable to set cookies in ODataClientSettings.BeforeRequest #613

Open Intagrit-F opened 5 years ago

Intagrit-F commented 5 years ago

@object With .Net core 2.1 we are able to add cookies to the request header. With .Net framework 4.7.2 it is not possible to add cookies to the request header

Version: Simple.OData.Client 5.6.2

Please have a look at the code below. (we also tried adding the cookie with f.Headers.TryAddWithoutValidation) settingsRequest.BeforeRequest = (f) => { f.Headers.Accept.Clear(); f.Headers.Add("Cookie", "mySSOcookie"); f.Headers.Add("Accept", "application/json"); f.Headers.Add("X-CSRF-Token", r.Headers.GetValues("x-csrf-token").First()); };

cookie correctly added in .Net core: netcore_screenprint

cookie missing in .Net framework: netframework_screenprint

object commented 5 years ago

Do you reuse HttpClient or create new instances for each request. What I've found it this: https://github.com/Microsoft/dotnet/issues/395

Not sure this is related to what you are experiencing. If not, I would advise you to build the library from sources and investigate/debug why cookies are lost in .NET FX.

Intagrit-F commented 5 years ago

Thanks for the quick response. We are not creating new instances each time. We do have 2 instances of the ODataClient:

The AfterResponse of the first "Logon" request is used to fill the BeforeRequest of the 2nd "request" client.

` List ssocookies = new List();

            //Get CSRF token and SSO cookie of the response headers
            settingsLogon.AfterResponse = (r) =>
            {
                ssocookies.AddRange(r.Headers.GetValues("Set-Cookie"));
                settingsRequest.BeforeRequest = (f) =>
                {
                    f.Headers.Accept.Clear();
                    f.Headers.Add("Accept", "application/json");
                    f.Headers.Add("X-CSRF-Token", r.Headers.GetValues("x-csrf-token").First());

                    foreach (String cookie in ssocookies)
                    {
                        if (cookie.Contains("SAP_SESSIONID_"))
                        {
                            f.Headers.Add("Cookie", cookie);
                        }
                    }
                };
            };`