0

我有一个让我保持登录的复选框,所以我使用 cookie 来实现它,但我的问题是我有 2 个要注销的链接,当我点击每个链接时,我没有被重定向到登录页面,因为在登录页面的负载上,我正在测试 cookie 是否已满且始终已满.. 因为注销时我无法清除 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
4

1 回答 1

1

Cookie 的棘手之处在于,即使您清除 cookie,它们仍然会持续存在。所以,我们唯一能做的就是让 cookie 信息过期。

调用 Cookies 集合的 Remove 方法会将 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,然后通过将过期时间设置为 Past 来使其过期

于 2013-10-04T14:49:01.540 回答