Cookies是始终过期 [英] Cookies are always expired

查看:107
本文介绍了Cookies是始终过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置一个cookie用:

I am setting a cookie with:

HttpCookie cookie = new HttpCookie("simpleorder");
cookie.Expires = DateTime.Now.AddYears(1);
cookie["order"] = carModel.ToString();
cookie["price"] = price.ToString();
Response.Cookies.Add(cookie);



但是,当我检查了几秒钟后,它已过期和过期日期设置为{01 -01-0001 00:00:00}。我试着检索

But when I check it a few seconds later it is expired and the expiration date is set to {01-01-0001 00:00:00}. I try to retrieve the code by

 HttpCookie cookie = Request.Cookies["simpleorder"];
 if (cookie != null && cookie.Expires > DateTime.Now)...

我不清除cookie的任何地方,所以我不知道为什么过期?

I don't clear the cookie any place, so I don't know why it expires?

推荐答案

这是常见的错误认识。请求Cookie集合代表包含在请求cookie头的cookie。这样的cookies不包含有关时,他们终止的任何信息。严格地说.NET理应使用两种不同类型(RequestCookie和ResponseCookie),而是选择使用两种情况下相同的类型。

This is common mis-understanding. The Request cookie collection represents the cookies included in the requests cookie header. Such cookies do not contain any info regarding when they expire. Strictly speaking .NET ought to have used two different types (RequestCookie and ResponseCookie) but instead chose to use the same type for both circumstances.

过期价值才有意义时,添加cookie来响应。

The Expires value only makes sense when adding cookies to the response.

这篇关于Cookies是始终过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆