Gio
Gio

Reputation: 565

How do I set cookie expiration to "session" in C#?

Self-Explanatory. In PHP, the solution would be to set the cookie expiration to 0; I'm unsure about C# since it requires a DateTime value.

Upvotes: 37

Views: 35054

Answers (4)

Lan99
Lan99

Reputation: 11

This seems to expire the cookie, rather than create a session cookie:

cookie.Expires = DateTime.MinValue

This creates a session cookie:

cookie.Expires = default(DateTime?)

Upvotes: 1

Vladimir Kurguzov
Vladimir Kurguzov

Reputation: 91

This doesn't work for me:

cookie.Expires = DateTime.MinValue

This does work:

cookie.Expires = default(DateTime?)

Upvotes: 5

o.k.w
o.k.w

Reputation: 25810

Do you mean any cookie or the session cookie? ASP.NET uses cookie by default for session 'management'.

Either you have the expiry or timeout in the web.config file, or programmatically set it using:
Session.Timeout = [x]; \\where [x] is in minutes

This can be called in different ways depending on your needs.

Upvotes: 0

Michael Petrotta
Michael Petrotta

Reputation: 60902

The docs for Cookie.Expires call it right out.

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

cookie.Expires = DateTime.MinValue

Upvotes: 57

Related Questions