0

I am using cookies for my website. I need to remove cookies immediately from browser for that i use this Code in C#

Code for removal of cookie

Response.Cookies["OptDepth"].Expires = DateTime.Now.AddYears(-30);

after execution of this code if i use this code

Code to check value of deleted cookie

Request.Cookies["OptDepth"].value;

then it gives me the value of specified cookie. I need to remove cookies immediately from browser. How can i do this.

4

3 回答 3

1

如果我们用 null 更改该 cookie 的名称,那么它就可以工作。

代码

Request.Cookies["OptDepth"].Name = null;

现在问题解决了。

于 2013-04-10T12:56:11.467 回答
0

使用简单代码将 cookie 替换为过期日期:

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

这是一个 msdn 详细链接。饼干

于 2013-04-10T12:49:59.807 回答
0

添加 cookie 时;

HttpCookie cookie = new HttpCookie("try");

                cookie.Values["foo"] = "foo";
                DateTime date = DateTime.Now.AddSeconds(-30);
                cookie.Expires = date;
                Response.Cookies.Add(cookie);

读取 cookie 时;

HttpCookie cookie = Request.Cookies["try"];

您将看到 cookie 为空。

于 2013-04-10T12:51:43.877 回答