开发者

Expire cookie at end of session OR at specific time?

开发者 https://www.devze.com 2023-04-10 02:37 出处:网络
Is it possible to have a cookie expire开发者_如何学编程 at the end of a session, or at a specific time?Yep! It\'s simple

Is it possible to have a cookie expire开发者_如何学编程 at the end of a session, or at a specific time?


Yep! It's simple

HttpCookie newCookie = new HttpCookie("myCookie");
newCookie.Expires = DateTime.Today.AddDays(1);

If you want the cookie to be for the session, set it to DateTime.MinValue. See the MSDN Documentation here for more info. Here's the excerpt:

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


since this is not possible with a single cookie i am sending two cookies. the auth cookie expires at the end of the session. the second cookie expires at a specific time. on each request i check the second cookie and if it is null i log the user out manually.


You can control cookie life time with Expires and Max-Age properties. Anyway if session is expired or you invalidate it impliclty, cookie associated with this session (for example jsessionId) are not valid anymore.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号