Clearing cookies

W

Water Cooler v2

I am practicing Cookies in .NET. Its working fine but when I want to
clear my cookies, the statement,

Response.Cookies.Clear

does not work. What gives?

Here's what I do:


(1) Login page

There's a "Remember me on this computer checkbox". When the user
presses the login button, I do this:


Sub btnLogin_Click(sender As Object, e As EventArgs)

if chkRemember.Checked = True Then
Dim ckUserName As HttpCookie = new HttpCookie("UserName")
ckUserName.value = Trim(txtUserName.Text)
ckUserName.Expires = System.DateTime.Now.AddYears(4)
Response.Cookies.Add(ckUserName)
ckUserName = Nothing

Dim ckPassword As HttpCookie = new HttpCookie("Password")
ckPassword.value = Trim(txtPassword.Text)
ckPassword.Expires = System.DateTime.Now.AddYears(4)
Response.Cookies.Add(ckPassword)
ckPassword = Nothing
End If

Response.Redirect("AnotherPage.aspx")
End Sub

Sub Page_Load(sender As Object, e As EventArgs)
if Not (Request.Cookies("UserName") Is Nothing And _
Request.Cookies("Password") Is Nothing) Then
Response.Redirect("AnotherPage.aspx")
End If
End Sub


(2) AnotherPage.aspx

Sub Page_Load(sender As Object, e As EventArgs)
If Not (Request.Cookies("UserName") Is Nothing And _
Request.Cookies("Password") Is Nothing) Then
Dim StrUserName As String =
Request.Cookies("UserName").value.ToString
Response.Write("Welcome, " & StrUserName & "!")
Response.Write("<br/><p>If you are not " & StrUserName & ",
then click <a href='ClearCookies.aspx'>here</a>.</p>")
Else
Response.Write("You are a cookieless bastard.")
End If
End Sub


(3) ClearCookies.aspx

Sub Page_Load(sender As Object, e As EventArgs)
Response.Cookies.Clear
Response.Redirect("index.aspx")
End Sub
 
A

Andrea Zani

Water Cooler v2 said:
I am practicing Cookies in .NET. Its working fine but when I want to
clear my cookies, the statement,

Response.Cookies.Clear

does not work. What gives?

Response.Cookies("Password").Expires=DateTime.Now.AddDays(-1)
Response.Cookies("...").Expires=DateTime.Now.AddDays(-1)
Response.Cookies("...").Expires=DateTime.Now.AddDays(-1)
 
J

John Rivers

indeed - and here is why:

Response.Cookies.Clear

just clears the current response buffer of cookie headers

which won't affect the browser's cookie store

to get the browser to dump the cookies
you have to set the cookies to expire in the past
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top