因此,我的一位同事使用以下代码从浏览器中“删除”了cookie。这应该可以,但是在加载页面后立即检查cookie,cookie仍然存在。这段代码有什么问题吗?还是有更大的问题?

protected void Page_Load(object sender, EventArgs e)
{
    HttpCookie aCookie;
    string cookieName;
    int limit = Request.Cookies.Count;

    for (int i = 0; i < limit; i++)
    {
        cookieName = Request.Cookies[i].Name;
        aCookie = new HttpCookie(cookieName);
        aCookie.Expires = DateTime.Now.AddDays(-1);

        if (cookieName != "Lang")
            Response.Cookies.Add(aCookie);
    }
    FormsAuthentication.SignOut();
    Response.Redirect("/default.aspx");
}

最佳答案

这是我用来杀死cookie的代码,它对我有用。

string cookieName;
    int limit = Request.Cookies.Count;


    for (int i = 0; i < limit; i++)
    {
        cookieName = Request.Cookies[i].Name;
        var cookie = new HttpCookie(cookieName);
        cookie.Value = "";
        cookie.Expires = DateTime.Now.AddDays(-3);
        //Only if HTTPS
        cookie.Secure = true;
        //Only if a domain is specified, and obviously, it should match the domain of the app
        cookie.Domain = "XYZ";

        Response.Cookies.Add(cookie);
    }
    FormsAuthentication.SignOut();
    Response.Redirect("/default.aspx");


确保将Cookie写入响应中。

10-02 02:08