I have a problem with persistent cookies in Internet Explorer, that is, I can set the cookies, but cannot make them persist. I'm using Internet Explorer 11, and have tried "Internet Options" -> "Advanced" -> "Reset", but it didn't help.
I've written this test code:
class Program {
public static void Main(string[] args) {
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:7777/");
listener.Start();
ThreadPool.QueueUserWorkItem((o) = > {
while (listener.IsListening) {
ThreadPool.QueueUserWorkItem((c) = > {
HandleRequest(c as HttpListenerContext);
}, listener.GetContext());
}
});
Console.Write("Press any key to quit . . . ");
Console.ReadKey(true);
listener.Stop();
listener.Close();
}
static void HandleRequest(HttpListenerContext ctx) {
var cookie = ctx.Request.Cookies["TestCookie"];
if (cookie == null) {
Console.WriteLine("Setting cookie...");
var expiryDate = DateTime.UtcNow.AddDays(360);
ctx.Response.Headers["Set-Cookie"] = "TestCookie=some_value; Path=/; Expires=" + expiryDate.ToString("ddd, dd-MMM-yyyy H:mm:ss") + " GMT; HttpOnly";
} else {
Console.WriteLine("Cookie: " + cookie);
}
ReturnString(ctx, "OK");
}
protected static void ReturnString(HttpListenerContext ctx, String s) {
try {
byte[] buf = Encoding.UTF8.GetBytes(s);
ctx.Response.ContentLength64 = buf.Length;
ctx.Response.OutputStream.Write(buf, 0, buf.Length);
} catch (Exception e) {
} // suppress any exceptions
finally {
// always close the stream
ctx.Response.OutputStream.Close();
}
}
}
Now, when accessing 127.0.0.1:7777
, I first get "Setting cookie", and then "Cookie: TestCookie=some_value" on all subsequent requests. In Chrome the cookie is persistent (I can close the browser, restart it, and still get "Cookie: TestCookie=some_value"), but this doesn't work in Internet Explorer. That is, when using IE, I get the "Setting cookie" on the first request every time I restart the browser. So the cookie is clearly not there anymore.
Why is this? Am I doing something wrong? Surely there must be some way to set persistent cookies in IE using a C# server?
Settings > View advanced settings. Scroll down to Cookies, and select Don't block cookies Internet Explorer In Internet Explorer, in the menu bar, select Tools > Internet options > Privacy > Advanced. Select Accept or Prompt under First-party Cookies, and Accept or Prompt under Third-party Cookies. Select OK.
Cookies, such as Persistent Cookies, are stored on a user's device to remember information, settings, preferences, or sign-on credentials. This saves time and effort when accessing a website. An expiration date is assigned to these cookies by the webserver.
All persistent cookies have an expiration date written into their code, but their duration can vary. According to the ePrivacy Directive, they should not last longer than 12 months, but in practice, they could remain on your device much longer if you do not take action.
Session cookies do not retain any information on your device or send information from your device. These cookies are deleted when the session expires or is terminated when the browser window is closed. Persistent cookies remain on the device until you erase them or they expire.
Hm, I found the answer. It's quite embarrasingly simple, really. When setting expiry date, I used the code
expiryDate.ToString("ddd, dd-MMM-yyyy H:mm:ss")
but of course this creates a localized representation of the date. And since I'm in Norway, the "ddd" created the day of the week in norwegian. And of course IE was not able to parse that.
This simple change solved the problem:
expiryDate.ToString("ddd, dd-MMM-yyyy H:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
So sorry for all your trouble, folks..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With