Avatar of indywebteam
indywebteam

asked on 

How can I make a cookie expire on browser exit.

Problem: User Closes borwser (IE) but Cookie still remains!, how can I make cookie expire on browser exit.
WebSite Technology: ASP.NET 2.0 Using C#.

RefCode:
HttpCookie LoginCookie = new HttpCookie("LoginCookie");
LoginCookie.Domain = ".xyz.com";
LoginCookie.Values["LASTVISIT"] = Request.Url.ToString();
LoginCookie.Values["REFID"]=Session["REFID"].ToString();
LoginCookie.Values["USER_ID"]=Session["USER_ID"].ToString();

// I tired -1....to +10...even tried to comment out whole line but nothing works
LoginCookie.Expires = System.DateTime.Now.AddDays(-1);
Response.Cookies.Add(LoginCookie);

Any help would be appreciated!

.NET ProgrammingASP.NETC#

Avatar of undefined
Last Comment
mrichmon
Avatar of mrichmon
mrichmon

If you don't set an expiration then the cookie is memory only and should be automatically deleted when the browser is closed (not the window, but exiting the browser - i.e. all windows)
Avatar of indywebteam
indywebteam

ASKER

I commented this line; "LoginCookie.Expires = System.DateTime.Now.AddDays(-1);"

HttpCookie LoginCookie = new HttpCookie("LoginCookie");
LoginCookie.Domain = ".xyz.com";
LoginCookie.Values["LASTVISIT"] = Request.Url.ToString();
LoginCookie.Values["REFID"]=Session["REFID"].ToString();
LoginCookie.Values["USER_ID"]=Session["USER_ID"].ToString();
// LoginCookie.Expires = System.DateTime.Now.AddDays(-1);
Response.Cookies.Add(LoginCookie);

and it still does not work.

??????
ASKER CERTIFIED SOLUTION
Avatar of mrichmon
mrichmon

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo