HttpListener and Cookies




I am using HttpListener.Response.AppendCookie as:

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

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

Any idea??


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

Similar Threads