gpt4 book ai didi

asp.net - 注销时删除 cookie

转载 作者:行者123 更新时间:2023-12-02 10:10:55 28 4
gpt4 key购买 nike

在主页的页面加载中,我设置了一个如下所示的 cookie:-

if (abc == true)
{
HttpCookie cookie = new HttpCookie("Administrator");
cookie.Value = "Admin";
cookie.Expires = DateTime.Now.AddDays(-1);
Response.SetCookie(cookie);
}

并将 cookie 用作:-

if (Request.Cookies["Administrator"] != null)
{
if (Request.Cookies["Administrator"].Value == "Admin")
//some code
}

注销时我希望此 cookie 过期或被删除。所以我写了:- Seesion.Abandon();

现在即使我注销后,当我重新登录主页时..Request.Cookies["Administrator"] 行仍然不为空。

奇怪...!请告知原因及解决办法。

最佳答案

来自MSDN DOCUMENTATION ,

You cannot directly delete a cookie on a user's computer. However, you can direct the user's browser to delete the cookie by setting the cookie's expiration date to a past date. The next time a user makes a request to a page within the domain or path that set the cookie, the browser will determine that the cookie has expired and remove it.

你可以这样做:

if (Request.Cookies["Administrator"] != null)
{
HttpCookie myCookie = new HttpCookie("Administrator");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}

关于asp.net - 注销时删除 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825669/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com