注销时清除cookie

时间:2013-10-04 14:41:51

标签: javascript asp.net vb.net cookies vb.net-2010

我有一个让我登录的复选框,所以我使用cookie来实现它,但我的问题是我有2个链接注销,当我点击每一个我没有被重定向到日志在页面中,因为在登录页面的负载我正在测试cookie是否已满并且它总是满的...因为当我退出时我无法清除cookie ...我尝试从后端调用一个函数清除饼干但它没有开火所以你能帮帮我吗?

   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

1 个答案:

答案 0 :(得分:1)

关于Cookies的棘手问题是,即使你清除了cookie,它们仍然存在。因此,我们唯一可以做的就是删除cookie信息。

调用Cookies集合的Remove方法会从服务器端的集合中删除cookie,因此cookie不会发送到客户端。但是,如果cookie已存在,则该方法不会从客户端中删除cookie。

You are misssing on following syntax:

Protected Sub btn_logout_Click(sender As Object, e As System.EventArgs) Handles 

您需要为事件处理程序提供参数..

使用以下代码清除Cookie:

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

希望这会有所帮助..

创建一个名称相同的新Cookie,然后将过期时间设置为过去