Storing Cookies from C#.NET Application

J

Jono

Hi Everyone,
Most tutorials I've seen address cookies from the server's perspective,
assuming that the client (usually a browser like Internet Explorer)
will manage the persistence. I am faced with writing a C#.NET client
that uses a web reference to a SOAP web service, and I need to persist
the cookies myself, or so it seems. The cookies stored in temporary
internet files - accessed each time I hit the web service help page
from IE during testing - don't appear to have any relationship with the
cookies that will be used by my homemade client. Is this correct, or am
I doing something silly? I'd really appreciate a code snippet if
possible.
Thanks,
Jono
 
L

Laurent Bugnion

Hi,
Hi Everyone,
Most tutorials I've seen address cookies from the server's perspective,
assuming that the client (usually a browser like Internet Explorer)
will manage the persistence. I am faced with writing a C#.NET client
that uses a web reference to a SOAP web service, and I need to persist
the cookies myself, or so it seems. The cookies stored in temporary
internet files - accessed each time I hit the web service help page
from IE during testing - don't appear to have any relationship with the
cookies that will be used by my homemade client. Is this correct, or am
I doing something silly? I'd really appreciate a code snippet if
possible.
Thanks,
Jono

I wrote about that referring to WPF, but it's the same for WinForms.
http://geekswithblogs.net/lbugnion/archive/2006/10/11/93743.aspx

HTH,
Laurent
 
J

Jono

Hi Laurent,
Thanks for your post but unless I misunderstood your blog, I am
approaching the problem from a different perspective. I have no access
to the web service code that runs on the server; all I have to do is
store all the cookies sent by the server and return them on subsequent
requests if they are valid (e.g. they match the server's domain and
path and the cookies haven't expired.) I am thinking of using the
CookieContainer object and serializing it to XML on the file system in
between runs of the client application so that cookies don't expire
prematurely (e.g. a cookie might be set to expire only at the end of
the week but the client application might restart many times before
that).
Regards,
Jono
 
L

Laurent Bugnion

Hi,
Hi Laurent,
Thanks for your post but unless I misunderstood your blog, I am
approaching the problem from a different perspective. I have no access
to the web service code that runs on the server; all I have to do is
store all the cookies sent by the server and return them on subsequent
requests if they are valid (e.g. they match the server's domain and
path and the cookies haven't expired.) I am thinking of using the
CookieContainer object and serializing it to XML on the file system in
between runs of the client application so that cookies don't expire
prematurely (e.g. a cookie might be set to expire only at the end of
the week but the client application might restart many times before
that).
Regards,
Jono

No, no, this code runs on the client:

public partial class Page1 : Page
{
private GetSessionIdService.Service1 m_oService
= new GetSessionIdService.Service1();

public Page1()
{
InitializeComponent();
m_oService.CookieContainer = new System.Net.CookieContainer();
}
}

You see the instantiation of the web service in my WPF application in
your case, it would be in the WinForms "OnInit" method, probably), and
then you must create and set the "CookieContainer" property of the web
service client.

If no CookieContainer is created, the session cookie sent by the web
service will be ignored, and a new session will be started on every call.

HTH.
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top