-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Cookies are not added to cookie container when running on iOS #2168
Comments
Based on what I see at https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.InvokeNativeHandler.cs This probably has something to do with the differences between the native handlers on iOS vs Android. If a redirect is involved, it might be related to #2059, although I wonder why the behavior would differ between mobile platforms. :( |
Thank you for your response and looking into the post.
The one can simply use |
@h-arshad RestSharp handles cookies without using an external It could also be that .NET on iOS has some differences concerning getting cookies from the container based on the URL. Basically, what RestSharp does: it creates a new cookie container for each request, unless the request itself has a cookie container. But, the container is not used for making a call. Instead, RestSharp adds cookie headers to the request. The code looks like this: public static void AddCookies(this CookieContainer cookieContainer, Uri uri, IEnumerable<string> cookiesHeader) {
foreach (var header in cookiesHeader) {
try {
cookieContainer.SetCookies(uri, header);
}
catch (CookieException) {
// Do not fail request if we cannot parse a cookie
}
}
} You see there that sometimes adding a cookie to the container fails. I am not sure if that's what happens on iOS. |
@h-arshad I have the same issue with RestClient not returning the cookies on MAUI for iOS. I was able to resolve it by passing an instance of HttpClient which had the I think setting it on the handler via RestClientOptions.ConfigureMessageHandler should probably work too, though i haven't tested it. The below code would probably also work with IHttpClientFactory, by using that to create the HttpClient instance(and configuring the CookieContainer), but again not tested.
@alexeyzimarev its feels like the Setting the CookieContainer on the request didn't fix the problem on iOS MAUI, neither did not setting
|
It's weird that the issue only affects one particular platform. RestSharp doesn't do any kind of magic with cookies, and the reason why it doesn't set the cookie container property of the handler is because otherwise handling cookies will deviate from what's already there when the container isn't used. Basically, after making a call, RestSharp tries to find cookie headers, collect the values, and add cookies to the response and to the // Parse all the cookies from the response and update the cookie jar with cookies
if (responseMessage.Headers.TryGetValues(KnownHeaders.SetCookie, out var cookiesHeader)) {
// ReSharper disable once PossibleMultipleEnumeration
cookieContainer.AddCookies(url, cookiesHeader);
// ReSharper disable once PossibleMultipleEnumeration
Options.CookieContainer?.AddCookies(url, cookiesHeader);
} |
@alexeyzimarev Thank you for the suggestion. I tried to make the request on sample code without initializing the cookie container, but it did not work. In fact, after looking into the response object and comparing the Android and iOS, I realized that on iOS the |
When using rest sharp in NET MAUI application development, for the same API call that is supposed to inject a cookie, in Android you will see the cookie but it is missing when running in iOS device.
Code Sample:
Expected behaviour:
The response cookie should be added inside the cookie container.
The text was updated successfully, but these errors were encountered: