Bug 11287 - Converting from System.net.Cookie to NSHttpCookie makes it a session cookie, dropping 'expired' date value
Summary: Converting from System.net.Cookie to NSHttpCookie makes it a session cookie, ...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-03-20 10:50 UTC by mayank.mnit
Modified: 2013-05-01 11:57 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description mayank.mnit 2013-03-20 10:50:40 UTC
Discussed in detail at http://stackoverflow.com/questions/15492894/cannot-add-a-cookie-to-nshttpcookiestorage-sharedstorage

I am trying to authenticate at a URL, fetch some cookies and save them on the simulator for future use.
The problem is that once i close the app on simulator, then re-run it, the cookies are gone. 
I figured out that when i converted the cookie to a NSHttpCookie, it lost the 'expired' field, and somehow isSessionCookie got True for that NSHTTPCookie - 

NSHttpCookie nsCookie = new NSHttpCookie(cookie);
Console.Out.WriteLine("System Cookie discard?" + cookie.Discard); // Output -  System Cookie discard?False
Console.Out.WriteLine("System Cookie expiry?" + cookie.Expires); // Output - System Cookie expiry?3/23/2013 7:46:59 PM
Console.Out.WriteLine("Is NsHttpCookie session cookie?" + nsCookie.IsSessionOnly);// Output - Is NsHttpCookie session cookie?True
Console.Out.WriteLine("NsHttpCookie expiry?" + nsCookie.ExpiresDate);// Output - NsHttpCookie expiry? 

So, its evident for some reason its treating this as a session cookie.
Can someone please take a look? Thanks!
Comment 1 Sebastien Pouliot 2013-03-20 21:09:40 UTC
That lack of expire is strange. I'll check on this.
Comment 2 Sebastien Pouliot 2013-03-20 21:44:54 UTC
"Domain" = ".collectedit.com";
"Expires" = 2013-04-21 01:35:09 +0000;
"Path" = "/";
"Name" = ".ASPXAUTH";
"Value" = "abc";
"Created" = 385522554;
"Discard" = "TRUE";

The NSDictionary, that you get back from the `Properties` property, contains the Expires date. However requesting the Expires property returns null. Also Discard was set to false when creating the instance. I'll check this again with more caffeine tomorrow...
Comment 3 Sebastien Pouliot 2013-03-21 10:41:11 UTC
This is what's given (NSDictionary) to create the cookie:

    Discard = FALSE;
    Domain = ".collectedit.com";
    Expires = "2013-04-21 12:57:44 +0000";
    Name = ".ASPXAUTH";
    Path = "/";
    Value = abc;
    Version = 0;

and this is the `Properties` (NSDictionary) that the NSHttpCookie gives back:

    Created = 385563686;
    Discard = TRUE;
    Domain = ".collectedit.com";
    Expires = "2013-04-21 12:57:44 +0000";
    Name = ".ASPXAUTH";
    Path = "/";
    Value = abc;

ExpiresDate stil returns `null` even if the property is there (but that's kind of normal as long as `Discard` is true). I'll need to confirm if the same happens from ObjC itself.
Comment 4 Sebastien Pouliot 2013-03-27 11:34:39 UTC
So it seems that asking for "Discard" to be `false` actually makes it `true` (like any value becomes true). This is unlike what's documented in [1] (it could be a documentation error, I'll do some testing and report it to Apple if wrong).

A workaround would be to copy/paste the code (that creates the dictionary from [2]) and remove/comment the lines about handling discard.

[1] https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

[2] https://github.com/mono/maccore/blob/master/src/Foundation/NSHttpCookie.cs#L82
Comment 5 Sebastien Pouliot 2013-05-01 11:57:13 UTC
Fixed in master 6791f77209356ad729087efba4803676b19a00d3