42

By default what will be the expiration time of a cookie added using C# code?

    HttpCookie myCookie= new HttpCookie("myCookie");
    myCookie.Value = txtCookie.Text;       
    // Add the cookie.
    Response.Cookies.Add(myCookie);
Sudha
  • 2,008
  • 6
  • 25
  • 53

2 Answers2

43

The default Expires value for a cookie is not a static time, but it creates a Session cookie. This will stay active until the user closes their browser/clears their cookies. You can override this as required.

From the linked page:

Setting the Expires property to MinValue makes this a session Cookie, which is its default value

CodingIntrigue
  • 65,670
  • 26
  • 159
  • 166
  • And the session cookie expires in 14 days by default (in Owin), see code here https://github.com/yreynhout/katana-clone/blob/master/src/Microsoft.Owin.Security.Cookies/CookieAuthenticationOptions.cs#L24 – Tomas Kubes Nov 22 '17 at 11:00
  • It's not specific to C#, right? It's default behaviour in browsers as well? Like when you do `document.cookie = 'asd=123'` it persists till you close the browser too unless you specified an expiration time? – Klesun May 01 '20 at 11:20
-12

20 minutes.
In IIS, click on your website, and go to Session State. In the second box (Cookie Settings), you can change the time out(in minutes).

enter image description here

Community
  • 1
  • 1
Cameron Castillo
  • 2,551
  • 7
  • 40
  • 65