3

I am trying to add multiple cookies to a clients web browser via my response.

First I add multiple cookie objects to the header using the System.Net.HttpListenerResponse.SetCookie method and then send back the response. According to the documentation this method "Adds or updates a Cookie in the collection of cookies sent with this response."

When I look at the cookies in the browsers developer tools I see only a single cookie gets added. My second cookies name and value seem to be getting appended to the value of my first cookie.

Chrome Developper Tools - Cookies Image

I wrote a simple console application to demonstrate the behavior all that you would need to do is add "127.0.0.1 website.test.com" to your host file and the URL will resolve.

using System.Net;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HttpListener listener = new HttpListener())
            {
                listener.Prefixes.Add(@"http://website.test.com/cookies/");
                listener.Start();
                HttpListenerContext context = listener.GetContext(); //Waits for an incoming request
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;
                response.SetCookie(new Cookie("name1", "value1"));
                response.SetCookie(new Cookie("name2", "value2"));
                response.StatusCode = (int)HttpStatusCode.OK;
                Stream responseStream = response.OutputStream;
                StreamWriter writer = new StreamWriter(responseStream);
                writer.Write("");
                response.Close();
            }
        }
    }
}

I also tried adding a semicolon to the end of my values. This corrected the issue with my second cookie being appended to my first cookies value but still only one cookie in the client.

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
Imran S
  • 33
  • 5

1 Answers1

4

HttpListenerResponse folds all cookies into a single Set-Cookie header:

HTTP/1.1 200 OK
Server: Microsoft-HTTPAPI/2.0
Set-Cookie: name1=value1, name2=value2

Folded cookies are ignored by modern browsers. Details are here: Is it possible to set more than one cookie with a single Set-Cookie?

Each cookie should be in a separate header:

HTTP/1.1 200 OK
Server: Microsoft-HTTPAPI/2.0
Set-Cookie: name1=value1
Set-Cookie: name2=value2

Add cookies as headers to HttpListenerResponse to get desired response:

static void Main(string[] args)
{
    using (HttpListener listener = new HttpListener())
    {
        listener.Prefixes.Add(@"http://website.test.com/cookies/");
        listener.Start();
        HttpListenerContext context = listener.GetContext();
        HttpListenerResponse response = context.Response;
        response.StatusCode = (int)HttpStatusCode.OK;
        response.AddHeader("Set-Cookie", "name1=value1");
        response.AppendHeader("Set-Cookie", "name2=value2");
        response.Close();
    }
}