0

I am trying to set cookie which expires in 10 sec in ASP.NET MVC3 Project. But its not exipring in 10 sec. Following is my code to set cookie:

HttpCookie loginCookie = new HttpCookie(cookieName, cookieValue);
loginCookie.Expires.AddSeconds(10);
Response.Cookies.Add(loginCookie);

While I checked in chrome settings it has following status:

Expires:    When the browsing session ends

Any suggestions, should i add somenthing in web.config file

1 Answers1

1

Looks like you aren't actually updating the value of loginCookie.Expires - you should probably be setting the time based on the current time (DateTime.Now) and use AddSeconds like this:

loginCookie.Expires = DateTime.Now.AddSeconds(10);

See https://msdn.microsoft.com/en-us/library/system.datetime.addseconds(v=vs.110).aspx:

"This method does not change the value of this DateTime. Instead, it returns a new DateTime whose value is the result of this operation."

Alex Walker
  • 2,337
  • 1
  • 17
  • 32