删除Cookie后,它会获得空值

时间:2016-07-13 06:44:33

标签: asp.net vb.net cookies

我在vb中有asp.net web应用程序。当用户登录时,会创建cookie,并将用户ID存储在cookie中。现在,当用户注销时,它应该从浏览器删除或删除cookie,但它没有发生。注销后,只有userid从浏览器中删除,但cookie仍然为null,这会在应用程序中产生问题。请帮助删除该特定cookie。

Protected Sub logout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles logout.Click
        Response.Cookies("chkusername").Expires = DateTime.Now.AddDays(-1)
        Response.Redirect("order-form.aspx")
End Sub 

要检查我在代码

下面使用的Cookie
Private Sub Online_Medicines_order_online_Default_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not HttpContext.Current.Request.Cookies("chkusername") Is Nothing Then
            userID.Text = Request.Cookies("chkusername").Value
        Else
            userID.Text = "No user Found"
        End If
End Sub

1 个答案:

答案 0 :(得分:1)

您没有将过期的Cookie添加到Response对象。

HttpCookie cookie = Request.Cookies("chkusername");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);