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.
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.