coockie设置Expires超时后,为什么还能获取coockie的值

anantnt203120 2007-08-06 02:19:25
我设置了一个登出按钮,执行
public void ExpiredCookie(System.Web.UI.Page page, string cookieName)
{
if (CheckCookie(page, cookieName) == true)
{
HttpCookie httpCookie = page.Request.Cookies[cookieName];

httpCookie.Expires = DateTime.Now.AddMinutes(-100);
page.Response.Cookies.Set(httpCookie);
}
}
可是再次跳转到系统内的页面,仍然可以获取coockie的值
public string GetCookieValue(System.Web.UI.Page page, string cookieName)
{
if (CheckCookie(page, cookieName) == true)
{
HttpCookie httpCookie = page.Request.Cookies[cookieName];

return httpCookie.Value;
}
else
{
return "";
}
}

请大家帮忙,我是跨主机头传递的coockie,问题和这有关系吗?
...全文
635 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
executemylove 2007-08-07
  • 打赏
  • 举报
回复
那个说E文的应该是大牛吧,但没怎么见过
honey52570 2007-08-07
  • 打赏
  • 举报
回复
mark
小程大爱 2007-08-07
  • 打赏
  • 举报
回复
if (Request.Cookies["cookieName"] != null)
{
HttpCookie cookies = Request.Cookies["cookieName"];
cookies.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookies);
}
我遇到过这样的问题,是这样解决的!应该是可以的!
xuyiazl 2007-08-07
  • 打赏
  • 举报
回复
up顶起

晓月解释的很清楚了~~~
batiraul 2007-08-07
  • 打赏
  • 举报
回复
草,这是csdn不是esdn!

e文那么好干嘛还用中文版的Studio搭aspx?

拜托甭用e文摆阔,给人家解释清楚不就得了,莫非不会中文或敲不了汉字或从别的地方拷贝的?
xiaoliangwh 2007-08-06
  • 打赏
  • 举报
回复
up
fanruinet 2007-08-06
  • 打赏
  • 举报
回复
And you mentioned the question in http://topic.csdn.net/t/20041111/10/3542500.html. I feel sorry that nobody can answer the simple question to the point.
Either the cookie nor the expiration date is stored on the client. When the client requests for a url, only the cookie names and the cookie values are sent to the server. The client will not sent the expiration date to the server. If the date expired, the client simply discards the cookie. So the server has no way to know the expiration date of a cookie.
fanruinet 2007-08-06
  • 打赏
  • 举报
回复
And the request response result(by ieHttpHeaders):
------------------The first run.
GET /CookieLab.aspx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: localhost:90
Connection: Keep-Alive
Cookie: .HERBAANONYMOUS=9tpqmHUSygEkAAAAYWQ4Mjk4MDctODBhNC00NzgxLTg5ZGYtZjI0YjY1ZTk0N2VlaGXxt40ZHT9NRGh4cDTxTNJMyBo1; shopperID=fancyf

HTTP/1.1 200 OK
Date: Mon, 06 Aug 2007 12:40:27 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=2zf0c2zs5w0zeu55duxv2wrt; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 1035

------------------Press the "Add Cookie" button.
POST /CookieLab.aspx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, */*
Referer: http://localhost:90/CookieLab.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: localhost:90
Content-Length: 230
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: .HERBAANONYMOUS=9tpqmHUSygEkAAAAYWQ4Mjk4MDctODBhNC00NzgxLTg5ZGYtZjI0YjY1ZTk0N2VlaGXxt40ZHT9NRGh4cDTxTNJMyBo1; shopperID=fancyf; ASP.NET_SessionId=2zf0c2zs5w0zeu55duxv2wrt

__VIEWSTATE=%2FwEPDwULLTE5ODU5MzI1NDIPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BQtObyBjb29raWVzIWRkZOmlKhKjkNGELhivLzRbjVv4w0zs&Button2=Add+Cookie&__EVENTVALIDATION=%2FwEWBQKF4rDGDgKu0%2BfvBgKM54rGBgK7q7GGCALWlM%2BbAnbadfzRJfBftW0deObdih6brXaq

HTTP/1.1 200 OK
Date: Mon, 06 Aug 2007 12:40:39 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727


---------Cookie is added here.
Set-Cookie: TestCookie=This is a test cookie.; path=/
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 1035


-----------Press the "Refresh" button.
POST /CookieLab.aspx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, */*
Referer: http://localhost:90/CookieLab.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: localhost:90
Content-Length: 232
Connection: Keep-Alive
Cache-Control: no-cache



---------------Cookie is sent here.
Cookie: .HERBAANONYMOUS=9tpqmHUSygEkAAAAYWQ4Mjk4MDctODBhNC00NzgxLTg5ZGYtZjI0YjY1ZTk0N2VlaGXxt40ZHT9NRGh4cDTxTNJMyBo1; shopperID=fancyf; ASP.NET_SessionId=2zf0c2zs5w0zeu55duxv2wrt; TestCookie=This is a test cookie.

__VIEWSTATE=%2FwEPDwULLTE5ODU5MzI1NDIPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BQtObyBjb29raWVzIWRkZOmlKhKjkNGELhivLzRbjVv4w0zs&Button3=Refresh+Page&__EVENTVALIDATION=%2FwEWBQKF4rDGDgKu0%2BfvBgKM54rGBgK7q7GGCALWlM%2BbAnbadfzRJfBftW0deObdih6brXaq

HTTP/1.1 200 OK
Date: Mon, 06 Aug 2007 12:40:46 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 1091




-------------Press the "Set Expired" button.
POST /CookieLab.aspx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, */*
Referer: http://localhost:90/CookieLab.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: localhost:90
Content-Length: 269
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: .HERBAANONYMOUS=9tpqmHUSygEkAAAAYWQ4Mjk4MDctODBhNC00NzgxLTg5ZGYtZjI0YjY1ZTk0N2VlaGXxt40ZHT9NRGh4cDTxTNJMyBo1; shopperID=fancyf; ASP.NET_SessionId=2zf0c2zs5w0zeu55duxv2wrt; TestCookie=This is a test cookie.

__VIEWSTATE=%2FwEPDwULLTE5ODU5MzI1NDIPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BSNDb29raWUgVmFsdWU6VGhpcyBpcyBhIHRlc3QgY29va2llLmRkZO1hWKUbMe2gVuehI2CTSK2Y52AC&btnTestCookie=Set+Expired&__EVENTVALIDATION=%2FwEWBQKc6q2mBwKu0%2BfvBgKM54rGBgK7q7GGCALWlM%2BbAtLyotj1TvA7A7mqWvJ7JHW6yy1b

HTTP/1.1 200 OK
Date: Mon, 06 Aug 2007 12:40:49 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727


----------The cookie is set expired here.
Set-Cookie: TestCookie=This is a test cookie.; expires=Mon, 06-Aug-2007 11:00:49 GMT; path=/
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 1091



------------Press "Refresh" button again.
POST /CookieLab.aspx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, */*
Referer: http://localhost:90/CookieLab.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: localhost:90
Content-Length: 264
Connection: Keep-Alive
Cache-Control: no-cache





------------The cookie is no longer sent from the client to the server.
Cookie: .HERBAANONYMOUS=9tpqmHUSygEkAAAAYWQ4Mjk4MDctODBhNC00NzgxLTg5ZGYtZjI0YjY1ZTk0N2VlaGXxt40ZHT9NRGh4cDTxTNJMyBo1; shopperID=fancyf; ASP.NET_SessionId=2zf0c2zs5w0zeu55duxv2wrt

__VIEWSTATE=%2FwEPDwULLTE5ODU5MzI1NDIPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BSNDb29raWUgVmFsdWU6VGhpcyBpcyBhIHRlc3QgY29va2llLmRkZO1hWKUbMe2gVuehI2CTSK2Y52AC&Button3=Refresh+Page&__EVENTVALIDATION=%2FwEWBQKc6q2mBwKu0%2BfvBgKM54rGBgK7q7GGCALWlM%2BbAtLyotj1TvA7A7mqWvJ7JHW6yy1b

HTTP/1.1 200 OK
Date: Mon, 06 Aug 2007 12:40:52 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=gb2312
Content-Length: 1035



The cookie has been deleted as expected. Would you like show your code here? Or you can test your request/response with ieHttpHeaders.
fanruinet 2007-08-06
  • 打赏
  • 举报
回复
I can't reproduce the bug. Here is my testing code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CookieLab : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie1 = Request.Cookies["TestCookie"];
if (cookie1 != null)
{
this.lblShowCookie.Text = "Cookie Value:" + cookie1.Value;
}
else
{
this.lblShowCookie.Text = "No cookies!";
}
}
protected void btnTestCookie_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["TestCookie"];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddMinutes(-100);
Response.Cookies.Set(cookie);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["TestCookie"];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddHours(10);
Response.Cookies.Set(cookie);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["TestCookie"];
if (cookie == null)
{
cookie = new HttpCookie("TestCookie", "This is a test cookie.");

Response.Cookies.Add(cookie);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//No Action, refresh only
}
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CookieLab.aspx.cs" Inherits="CookieLab" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblShowCookie" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnTestCookie" runat="server" Text="Set Expired" OnClick="btnTestCookie_Click" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Set Unexpired" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Add Cookie" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Refresh Page" /></div>
</form>
</body>
</html>

LutzMark 2007-08-06
  • 打赏
  • 举报
回复
还没回传当然能获值
Jinglecat 2007-08-06
  • 打赏
  • 举报
回复
可是再次跳转到系统内的页面
=======
设置了过期时间之后,需要先输出客户端一次,而不是直接重定向到其他页面,

否则,虽然你设置了国旗时间,但是此Cookie此时依然存在 Resonse.Cookies 中

过期时间是由客户端“感应”的
cpp2017 2007-08-06
  • 打赏
  • 举报
回复
1、.net 有自己的安全认证方式比如form认证。
whw123456 2007-08-06
  • 打赏
  • 举报
回复
学习``
anantnt203120 2007-08-06
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20041111/10/3542500.html
anantnt203120 2007-08-06
  • 打赏
  • 举报
回复
关闭浏览器仍然可以获取coockie的值

具体问题就像这个帖子讨论的一样

看来是大家都遇到过的,搜了很久,没找到解决办法。
anantnt203120 2007-08-06
  • 打赏
  • 举报
回复
那这样如何能实现“退出登录”的效果,请问各位是如何实现“用户退出”的。
cpp2017 2007-08-06
  • 打赏
  • 举报
回复
不是不起作用.关闭浏览器就没了.
yaoshuwen 2007-08-06
  • 打赏
  • 举报
回复
cookie有时候时间设置确实不起作用
Jinglecat 2007-08-06
  • 打赏
  • 举报
回复
何必这么吝啬呢,让时光多倒退些吧

try ->

httpCookie.Expires = DateTime.Now.AddMinutes(-100);

>>

httpCookie.Expires = DateTime.Now.AddDays(-100);
michaelowenii 2007-08-06
  • 打赏
  • 举报
回复
如果将 Expires 属性设置为 MinValue(这是它的默认值),则会使之成为会话 Cookie。


http://bbs.chinaunix.net/archiver/?tid-924512.html
加载更多回复(11)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧