Cookie Expire

B

Big E

I set a cookie at a login screen that sets various information. When users
are finished with the application most of them close the browser instead of
clicking logoff.
How do I clear or remove cookies when the shut down the broswer. I know how
to remove them like this:

Private Sub RemoveCookies()
If Request.QueryString("AcceptsCookies") Is Nothing Then
Response.Cookies("loginInfo").Expires = DateTime.Now.AddDays(-1)
End If
End Sub

But how do I force this when the browser closes.

Thanks.

Big E
 
S

Scott M.

If the user has closed their browser, it's too late for you to do anything
since removing the cookie is a function of the browser. The browser will
remove a cookie when instructed to do so by the server, but if the browser
is already closed it's too late. You may want to make sure that when you
set the cookie in the first place that it has a 1 day expiration date so
that if the user comes back the next day, the browser will know that the
cookie from yesterday is no longer valid.
 
S

Stefan

In the Global.asax file there are some "speciel" sections where you can
handle your problem
I think it's OnSession_End where you can write your code.
But you don't have set the Cookie expire to yesterday.
Use the following code instead
Response.Cookies("loginInfo").Expires = DateTime.Now.

Cheers
Stefan
 
K

Kevin Spencer

If you don't set the expiration date on the cookie, it expires as soon as
the browser Session ends (when they close their browser).

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Scott M.

Stefan said:
In the Global.asax file there are some "speciel" sections where you can
handle your problem
I think it's OnSession_End where you can write your code.
But you don't have set the Cookie expire to yesterday.
Use the following code instead
Response.Cookies("loginInfo").Expires = DateTime.Now.

But again, the information about the expiration date must be transmitted to
the browser so that it can remove the cookie. If they have closed their
browser, it won't get the information about the expiration in your code
above becaue there is no browser to respond to.

Also, when you close your browser, the server is not alerted, so after 20
minutes (the default session timeout period), the code you have above will
run. In other words, your code won't until for 20 minutes AFTER the browser
has closed. Your code would be fine if the user uses the "log off" feature
of the site because then, the code can call session.abandon and your code
will run immediately. But the OP stated that his/her problem is that users
aren't logging off, they are just shutting their browser down.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top