Reputation: 1115
i have a keep me logged in check box so i'm using cookies to implement it, but my problem is that i have 2 links to log out and when i click on each one i'm not being redirected to the log in page because on the load of the log in page i'm testing if the cookie is full and it's always full.. because when logging out i'm not able to clear the cookie... i tried calling a function from back end to clear the cookie but it did not fire so can you help me please?
Protected Sub btn_login_Click(sender As Object, e As System.EventArgs) Handles btn_login.Click
If Session("valid") Then
If (loginkeeping.Checked = True) Then
Dim mycookie As New HttpCookie("LoginDetail")
Dim username As TextBox = Page.FindControl("username_txtt")
Dim password As TextBox = Page.FindControl("passwordsignup")
mycookie.Values("Username") = username.Text.Trim()
mycookie.Values("Password") = password.Text.Trim()
'mycookie.Expires = System.DateTime.Now.AddDays(1)
Response.Cookies.Add(mycookie)
End If
Response.Redirect("Insert_Content.aspx")
End If
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not (Response.Cookies("LoginDetail") Is Nothing) Then
Response.Redirect("Insert_Content.aspx")
End If
End Sub
asp.net:
<ul id="nav" class="fl">
<li class="v-sep">
<asp:HyperLink ID="logUser" runat="server" class="round button dark menu-user image-left"></asp:HyperLink>
<ul>
<li><asp:HyperLink ID="Myprofile" NavigateUrl="~/Profile.aspx" runat="server">My Profile</asp:HyperLink>
<li><asp:HyperLink ID="changePAss" NavigateUrl="~/Change_Password.aspx" runat="server">Change Password</asp:HyperLink></li>
<li><asp:HyperLink ID="logout" NavigateUrl="~/LOGIN.aspx" runat="server" onClick="logoutt">Log out</asp:HyperLink></li>
</ul>
</li>
<li><a href="LOGIN.aspx" runat="server" class="round button dark menu-logoff image-left" onClick="logoutt">Log out</a></li>
</ul> <!-- end nav -->
Public Sub logoutt()
Dim CookieName As HttpCookie = Request.Cookies("username")
CookieName.Expires.AddMilliseconds(1)
CookieName.Value = Nothing
Response.Cookies.Add(CookieName)
End Sub
Upvotes: 0
Views: 2402
Reputation: 4753
The tricky thing about Cookies is even if you clear cookies they still Persist. So, the only thing we can do to remove cookie information is by expiring them.
Calling the Remove method of the Cookies collection removes the cookie from the collection on the server side, so the cookie will not be sent to the client. However, the method does not remove the cookie from the client if it already exists there.
You are misssing on following syntax:
Protected Sub btn_logout_Click(sender As Object, e As System.EventArgs) Handles
You need to provide arguments to the event handler..
Use the following code to clear cookies:
If (Not Request.Cookies("username") Is Nothing) Then
Dim myCookie As HttpCookie
myCookie = New HttpCookie("username")
myCookie.Expires = DateTime.Now.AddDays(-1D)
Response.Cookies.Add(myCookie)
End If
Hope this helps..
Create a new cookie with the same name and then expire it by setting expiry time to Past
Upvotes: 1