Bug 4990 - Creating an NSHttpCookie from System.Net.Cookie does not behave as expected
Summary: Creating an NSHttpCookie from System.Net.Cookie does not behave as expected
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2012-05-09 21:55 UTC by James Bright
Modified: 2012-05-15 23:02 UTC (History)
2 users (show)

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


Attachments
update monotouch.dll (3.05 MB, application/octet-stream)
2012-05-15 08:38 UTC, Sebastien Pouliot
Details


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:
Status:
RESOLVED FIXED

Description James Bright 2012-05-09 21:55:05 UTC
I have a WebApi based back end (.NET) that will issue the standard Forms Based Authentication (FBA) ticket upon successful login. I am calling my service from an HttpWebRequest. This all works as expected. On receiving the JSON response, I intercept the .ASPXAUTH cookie and save it for later use. My application is a hybrid UIWebView & native. 

When making subsequent request to the server to get plain HTML, I use NSHttpCookieStorage to set the cookie. I have found this:

authCookie = new NSHttpCookie(ApplicationContext.AuthenticationCookie); 
NSHttpCookieStorage.SharedStorage.SetCookie(authCookie);
WebView.LoadRequest(mutableRequest);

Does not work. (The ApplicationContext.AuthenticationCookie is the System.Net.Cookie that I saved when the user successfully logged in). However, if I change it to this:

authCookie = new NSHttpCookie(".ASPXAUTH", ApplicationContext.AuthenticationCookie.Value, "/", ".collectedit.com"); 
NSHttpCookieStorage.SharedStorage.SetCookie(authCookie);
WebView.LoadRequest(mutableRequest);

It works. I did double check that the Name and the Domain of the System.Net.Cookie matched. Believe me, this threw me for a bit. It appears that something in the returned auth cookie is making it so that the cookie is not treated the same way.
Comment 1 Sebastien Pouliot 2012-05-10 12:08:42 UTC
weird, can you give me a value of `ApplicationContext.AuthenticationCookie` that shows the behavior ? I'll be able to check if there's anything internal that differs from the 2 ctors based on this information. Thanks.
Comment 5 Sebastien Pouliot 2012-05-14 23:05:05 UTC
There's two main difference between the cookies. 

1. `HttpOnly` cannot be set (but that should not affect the client app) and;
2. an empty "" Port (in .NET) creates one entry in PortList (instead of being empty) with a "0" value.

Looking into #2...
Comment 6 Sebastien Pouliot 2012-05-15 08:38:14 UTC
Created attachment 1890 [details]
update monotouch.dll

Attached is a fixed copy of monotouch.dll that does not create an empty PathList entry when convertie a .NET cookie to a NSHttpCookie. I would be grateful if you could try this and confirm if it fix your issue.

To use the attached assembly (on top of MonoTouch 5.2.11) do:

1) backup your /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll and /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll.mdb files

2) copy the attached file to /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll

3) remove the /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll.mdb symbols (they won't match anymore)

4) clean, rebuild and test your application
Comment 8 James Bright 2012-05-15 22:19:45 UTC
Tested the following:

1) original monotouch.dll explicitly setting the cookie works
2) original monotouch.dll using the .net version of cookie fails
3) attached monotouch.dll using the .net version of the cookie works

By "works" I mean that I was also looking at my IIS logs that also included logging cookie values. I could see the .ASPXAUTH cookie making it in trials 1 & 3, but not in 2. So I think you've fixed it. 

I have to fix something in my code, but this should now allow me to have authenticated web pages in UIWebView *and also* authenticated raw JSON calls using the same mechanism. For anyone using [Authenticated] on the MVC controllers, this is gold Jerry, gold.

I assume that I should revert my patch and wait for the proper release cycle...
Comment 9 Sebastien Pouliot 2012-05-15 22:34:13 UTC
Thanks for confirming.  We started the cycle for 5.2.12 but I'll look if it's not too late (tags are not done) to include it in the upcoming beta.
Comment 10 Sebastien Pouliot 2012-05-15 22:57:20 UTC
Fixed.

5.2-series: 07e6cffc3fb63abe0077d3bb995807b4df197f4c
master: 4f10c011d9bf5f1f0e4fcbe477564b8358eb23d4

QA: a unit test was added in both branch

James: Thanks for testing, I'll let you know asap for 5.2.12 status (in the meantime you can keep the hotfix on top of 5.2.11).
Comment 11 James Bright 2012-05-15 23:02:01 UTC
Excellent. Thanks Sebastien.