views:

39

answers:

3

Hello,

I'm working on a webservice that needs to be contacted from different clients.
Since I never know what language the client will be using I was wondering if every language can retrieve and pass a Class of the type "System.Net.CookieContainer".

Ex: I have a webservice that authenticates a client and returns a CookieContainer (this can be passed in the following requests). This way I can keep that session active.

Client 1 will use .net (this works obviously).
Client 2 will use php (will this work?). Client 3 will use java (will this work?).

The reason why I would like to use this is because a user can authenticate with our webservice and in the following requests he doesn't need to do the authentication again.

After some searching arround I've found the CookieContainer solution.

Any thoughts?

Cheers, M.

+1  A: 

If you return a class from web service, then it will be serialised to XML. Because it is a .net class .net languages will be able to deserialise it back to an actual class. Other languages can only access the serialised class to get at the data or possible desialise it to a custom class that matches the xsd complextype for the class, if the language supports that feature. However this will only bring in any data in the class, any method that are part of the class are not actually serialised, it only seems that they are in .net becuase when the class is deserialised .net will instantiate the relevent class and populate the properties with the data from the serialised class.

Ben Robinson
A: 

It is unclear what you mean by “returns a CookieContainer”. A webservice is, as the name implies, a service on the web — on the web, we have webrequests (HTTP requests) and they return an HTTP response. An HTTP response may or may not include cookies which the client will remember for later requests. So far, this doesn’t depend on any programming language.

You didn’t specify what your webservice is written in, but since you are referring to a .NET type, presumably it will be some .NET language, perhaps C#/ASP.NET. I am unsure how you would “return a CookieContainer” in that; normally you would set your cookies by using the SetCookie(...) method on your HttpResponse object. If you do it this way, PHP and Java will see the cookies as normal HTTP cookies and won’t have to care that they come from .NET. In a PHP client, for example, you would likely use HttpRequest::send to invoke the web request, and then you can use HttpRequest::getResponseCookies to retrieve the cookies.

Timwi
+1  A: 

It sounds like you just want people to be able to authenticate before they can use your webservice. For this you don't need to pass a CookieContainer class back and forth, you can just use forms authentication to let people log in.

Once someone passes the correct credentials through some login function you can call FormsAuthentication.SetAuthCookie to set their session information. Then on each call you can check to make sure they are authenticated with User.Identity.IsAuthenticated

Now, if your client is something other than a web browser and doesn't auto-magically handle cookies already, then you'll need to create a way to store and manage cookies. If your client is written in .NET then you can use the CookieContainer class to contain and manage the authentication cookie sent back from your webservice.

Justin
thx, this looks what I'll need. I thought a webservice created a new session every time you contacted it. but I didn't knew it kept the authentication.That would be great. I'll try it asap.
Sem Dendoncker
Just make sure your webservice has session state enabled and you should be all set.
Justin