Windows Phone 7 - Cookies not sent to WCF service

I have a bunch of WCF services at a domain:

  • AuthenticationService (the standard MS one, over HTTPS)
  • AppService (HTTP)

Usually, I call the Authentication Service and a cookie is returned. For desktop applications, I detach the cookie and reattach it with every new service call to the AppService which exposes the meat of my API.

Silverlight in the browser automatically attaches the cookie in all calls to the domain. I expected the phone to do the same.

It doesn't.

Access to the headers is not supported on the phone, so manual manipulation is out. I wonder if its because some bright spark at MS thought that the phone should enforce that cookies are only reattached to HTTPS endpoints at the same domain or something...

Help!!

This is a nightmare to troubleshoot since the phone doesn't support the other major major helpful setting; ignoring self-signed certificates.

Thanks,

Luke

** UPDATE **

While I'm following up the method using CookieContainer I must point out that even though the Add method on the Headers collection is missing in Silverlight, one can still add headers using the indexer.

See http://cisforcoder.wordpress.com/2010/12/01/how-to-implement-basic-http-authentication-in-wcf-on-windows-phone-7/

** UPDATE 2 **

CookieContainer can be set as per Lex answer. I'm now stuck and continuing to investigate an ArgumentNullException thrown from within the WCF client upon References.cs EndInvoke. My server shows no sign of receiving the call.

Two key calls on the stack are:

System.Net.Browser.HttpWebRequestHelper.ParseHeaders

And

MS.Internal.InternalWebRequest.OnDownloadFailed

FINAL UPDATE

The ArgumentNullException seems to be thrown when called against a server with a self-signed certificate.

However, there's something odd with the emulator/SDK. I have had this exception against all my servers, even those with no SSL, and one with an issued certificate.

I've also had problems that have been resolved only by a local reboot. So I think my problems have been a result of having the right code, but thinking it was wrong because of other problems in the SDK.

Not sure what advice to give, except to be mistrusting of exceptions stemming from the WP7 WCF stack, particularly EndpointNotFoundException and ArgumentNullException, and to have a full framework test client app around as a sanity check.

Luke

Answers


In desktop SL, I do it by sharing cookie container between service proxies in client http stack. Did not try this on WP7, but you may check this out.

First, you need to insert <httpCookieContainer/> in your client-side binding configuration (usually in ServiceReferences.ClientConfig).

In code, switch to client http stack.

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

Then you need to create a CookieContainer instance and assign it to all service proxies.

var cc = new CookieContainer();
var service1 = new ServiceReference1.MyService1Client { CookieContainer = cc };
var service2 = new ServiceReference2.MyService2Client { CookieContainer = cc };

Now, when your cookie container receives cookie, it will reuse it for all web services. Make sure, that cookie comes with correct Path setting.


Need Your Help

Date format retrieved from a service

java javascript android json

I'm working on getting Json objects from a service to a List View in Android... the date format looks like this "/Date(1354222800000+0300)/" ... how can I change it to a readable format?

Ideal way to pull data (JSON) from a service using Monotouch and iOS?

ios json web-services asynchronous monotouch

I have an iPhone app which is pulling just about all it's data from ASP.NET MVC services.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.