0

I have an Asp.Net website, and have 2 cookies.

I create these cookies by this code;

public static void CreateCookie(string Cookie)
{
  HttpCookie cookie = new HttpCookie(Cookie);
  cookie.Expires = DateTime.Now.AddYears(1);
  HttpContext.Current.Response.Cookies.Add(cookie);
}

CreateCookie("Cookie1");
CreateCookie("Cookie2");

So both cookies' lifetime is 1 year, as I see on Chrome.

But when I check 1 day later, I see that Cookie1 is automatically deleted,
although I check the cookies without entering the website and just checking on Chrome.

I cannot prevent this. Why this occures ?
And how can I fix this ?

Update :
This error happens only on my own website.
And I'm going to check this on another pc and another browser.

Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
  • The inverse is [discussed here](http://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies), I'm pretty certain you have a privacy setting or plugin active which removes cookies regardless. Please elaborate on whether this only happens on this browser, on this machine, on this site, and so on as explained in [ask]. – CodeCaster Jan 10 '17 at 14:36
  • Okey I have added update. – Bengi Besçeli Jan 10 '17 at 14:51
  • But my browser doesn't have any extension loaded. – Bengi Besçeli Jan 10 '17 at 15:03
  • Are you accessing the site the same way? Note that cookies created for `localhost` will not be presented when you access the site via `127.0.0.1`, for example. – John Wu Jan 11 '17 at 02:47

2 Answers2

0

Try linking the cookie to the session via:

HttpCookie authCookie = new HttpCookie(Cookie, Session.SessionID);
Steve Padmore
  • 1,710
  • 1
  • 12
  • 18
0

You do not set a value to the cookies. Adding a cookie without value will make the browser delete the cookie with the given name, if it exists.

Use a value:

public static void CreateCookie(string Cookie, string value)
{
  HttpCookie cookie = new HttpCookie(Cookie, value);
  cookie.Expires = DateTime.Now.AddYears(1);
  HttpContext.Current.Response.Cookies.Add(cookie);
}

CreateCookie("Cookie1", "Hello");
CreateCookie("Cookie2", "world");
NineBerry
  • 26,306
  • 3
  • 62
  • 93