Cookie not deleting

本文关键字:deleting not Cookie | 更新日期: 2023-09-27 17:53:34

System.Web.UI.Page oPageMy cookie未删除。我看了几篇文章,一切看起来都很好,只是当我在resopnse中浏览Visual Studio(或者只是在localhost下运行(并点击按钮时,我的cookie就保留了下来。

不管它值多少钱,我都在使用Visual Studio 2012和.Net 4.0。我正在本地主机上使用默认IE进行调试(Win7/64上的v9带有所有最新更新(。

public static void LoginUser(String strEmail, int iId, int iKeepDays)
{
    HttpCookie oCookie = new HttpCookie("myCookie");
    // Set the cookie value.
    oCookie.Secure = false;
    oCookie["Id"] = iId.ToString();
    oCookie["Email"] = strEmail;
    oCookie.Expires = DateTime.Now.AddDays(iKeepDays);
    // Add the cookie.
    HttpContext.Current.Response.Cookies.Add(oCookie);
}
public static void LogoutUser(System.Web.UI.Page oPage)
{
    // Get the cookie.
    HttpCookie oCookie = new HttpCookie("myCookie");
    oCookie = HttpContext.Current.Request.Cookies["myCookie"];
    if (null != oCookie)
    {
        // Remove the cookie.
        cCookies.RemoveCookie("myCookie");
        // Go back to the home page.
        if (oPage.IsCallback)
            ASPxWebControl.RedirectOnCallback("/");
        else
            HttpContext.Current.Response.Redirect("/");
    }
}
/// <summary>
/// This function will be used to remove cookies value 
/// </summary>
/// <param name="key"></param>
public static void RemoveCookie(String key)
{
    //get cookies value 
    HttpCookie oCookie = null;
    if (null != HttpContext.Current.Request.Cookies[key])
    {
        oCookie = HttpContext.Current.Request.Cookies[key];
        // You cannt directly delte cookie you should set its expiry date to earlier date 
        oCookie.Expires = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Cookies.Add(oCookie);
    }
}

Cookie not deleting

现在我正在写并弄清楚了答案,答案似乎很明显,但我可以说,无论答案是否显而易见,都不是那么容易得到的。

上面的代码在服务器上执行,但cookie的删除发生在客户端上。执行必须转移到客户端,然后返回到服务器,以便服务器识别cookie已被删除。

我在同一个注销调用中读取数据,只是在不同的函数中。由于已接受的实践状态是重置cookie,因此函数会将cookie写回。cookie已被删除,然后又返回。它甚至有了一个新的文件名。(我打开了隐藏的cookie文件夹。(

我的解决方案是将登录状态传递给另一个函数。这解决了饼干的问题。

cCookies.RemoveCookie("myCookie");行不调用RemoveCookie方法。该行应改为RemoveCookie("myCookie");