HttpListener and Cookies

  • Thread starter Thread starter Rok
  • Start date Start date
R

Rok

Hi,

I am using HttpListener.Response.AppendCookie as:

public void SetCookie(string name, string value, DateTime
expiration)
{
// add new cookie to HTTP response
Cookie ck = new Cookie(name, value);
ck.Expires = expiration;
ck.Discard = false;
HttpListenerContext.Response.AppendCookie(ck);
}

But what I am receiving in a browser is a cookie that has expiry of
exactly 59 seconds, regardless of what I set in ck.Expires. So it is
always a session cookie which disappears as soon as i close the
browser. I used a IEWatch to see the request/response headers and the
response header only returns a cookie and a value, no expiry. Is that a
microsoft bug or am i doing something wrong?
 
I figured out what the problem is. HttpListenerResponse object has headers
arranged as keys, meaning each key is unique. For setting multiple cookies,
HttpListenerResponse would have to set multiple Set-Cookie headers which it
cannot, that's why my first cookie that I add to the response cookie
collection overrides all subsequent. if you're adding just name=value
non-persistent cookies, it works fine. As soon you want to add two cookies
with different expiry than you have a problem I still don't know the solution
to.

Any idea??

Rok
 
Back
Top