I am practicing Cookies in .NET. Its working fine but when I want to clear my cookies, the statement,
does not work. What gives?Code:Response.Cookies.Clear
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:
Code: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
Code: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 *******.") End If End Sub
(3) ClearCookies.aspx
Code:Sub Page_Load(sender As Object, e As EventArgs) Response.Cookies.Clear Response.Redirect("index.aspx") End Sub


Reply With Quote