这个问题已经在这里有了答案:




已关闭8年。






我要在用户注销时删除Cookie。

这是我的代码:

 if (HttpContext.Current.Request.Cookies["currentUser"] != null)
 {
     DeleteCookie(HttpContext.Current.Request.Cookies["currentUser"]);
 }


       public void DeleteCookie(HttpCookie httpCookie)
        {
            try
            {
                httpCookie.Value = null;
                httpCookie.Expires = DateTime.Now.AddMinutes(-20);
                HttpContext.Current.Request.Cookies.Add(httpCookie);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

但这是行不通的。你有什么建议吗?

最佳答案

 HttpCookie currentUserCookie = HttpContext.Current.Request.Cookies["currentUser"];
 HttpContext.Current.Response.Cookies.Remove("currentUser");
 currentUserCookie.Expires = DateTime.Now.AddDays(-10);
 currentUserCookie.Value = null;
 HttpContext.Current.Response.SetCookie(currentUserCookie);

有用。

07-26 09:36