2011-12-29 115 views
1

我使用此代码来设置Cookie。它在Firefox中完美运行。但不是在IE9中。Cookie未在IE9中设置

下面是代码:

HttpCookie visitorCookie = new HttpCookie("VisitorCity", DdlCity.SelectedItem.Text) 
{Expires = DateTime.Now.AddMonths(1)}; 
HttpContext.Current.Request.Cookies.Add(visitorCookie); // Add it to the header 

回答

3

通常,当你想设置你应该把它添加到响应,而不是请求一个cookie:

HttpContext.Current.Response.Cookies.Add(visitorCookie); 

这是客户端浏览器,随后发送的HTTP请求时,将附加的cookie作为请求头。

2

尝试加入您的Cookie到HttpContext.Current.Response而不是索取。

您可以检查从Request对象的饼干,但你需要把它们设置在响应

HttpCookie visitorCookie = new HttpCookie("VisitorCity", DdlCity.SelectedItem.Text) 
{Expires = DateTime.Now.AddMonths(1)}; 
HttpContext.Current.Response.Cookies.Add(visitorCookie); // Add it to the header 
+0

我解决了这个问题,通过改变请求来响应并转到=> Internet选项=>高级选项卡=>重置 – Humayoo 2011-12-30 03:59:18

0

我与IE有同样的问题。发现用户的IE首选项禁用了Cookie。先检查一下!