2016-07-13 46 views
0

我在vb中有asp.net web應用程序。當用戶登錄Cookie被創建並將用戶ID存儲在cookie中時。現在,當用戶註銷它應該刪除或從瀏覽器中刪除cookie,但它沒有發生。註銷後,只會從瀏覽器中刪除用戶標識,但Cookie仍爲空,從而在應用程序中產生問題。請幫助刪除該特定的cookie。刪除cookies後,它的值爲空

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 

要檢查我用下面的代碼

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 
+0

什麼樣的問題? – Imad

+0

@Imad就像我已經在我的代碼中放置條件,如果沒有cookie,那麼它應該重定向到登錄頁面,如果有cookie,那麼繼續。但是因爲這裏cookie(「chkusername」)獲得空值,所以它在沒有用戶ID的情況下繼續 – SUN

回答

1

您不添加過期的cookie來Response對象的cookie。

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