2011-09-21 26 views
0

我一直在寻找解决这个问题的办法,但我没有办法。顺便说一句,我不明白我的问题的原因。Asp.net HttpCookie读/写问题

的问题是:

我的web应用程序有一个登录页面,并会记录从cookie的用户ID。它工作之前,但5-6天前,因为改变了一些东西,它不适用于IE浏览器。现在它不适用于任何浏览器。

我可以在Chrome中看到cookie。当Internet Explorer开发人员工具有时cookie的书面看了,但仍无法通过IE阅读

我的web应用程序在Windows Server 2008 R2 BTW

设置我的web.config:

<httpCookies domain=".domainname.com" httpOnlyCookies="false" requireSSL="false" /> 

这里是我的setcookie代码

<!-- language: c# --> 
string uId = "userID"; 
DateTime expireDate = DateTime.Now.AddDays(3); 
HttpContext.Current.Response.Cookies["cookieName"]["uID"] = uId; 
HttpCookie aCookie = new HttpCookie("cookieName"); 

aCookie.Values["uID"] = uId; 
aCookie.Path = "/"; 
aCookie.Expires = expireDate; 
aCookie.HttpOnly = false; 
aCookie.Domain = "domainname.com"; 
aCookie.Name = "cookieName"; 
HttpContext.Current.Response.Cookies.Add(aCookie); 

这的getCookie代码

<!-- language: c# --> 
if (HttpContext.Current.Request.Cookies["cookieName"] != null) 
{ 
    System.Collections.Specialized.NameValueCollection UserInfoCookieCollection; 
    UserInfoCookieCollection = HttpContext.Current.Request.Cookies["cookieName"].Values; 
    userID = HttpContext.Current.Server.HtmlEncode(UserInfoCookieCollection["uID"]); 
} 

的情况是:

尝试登录

的setcookie方法所触发的setcookie方法

末有两种曲奇 “cookieName” 和 “ASP.NET会话ID”

触发GetCookie方法

只有“ASP.NET会话ID”和会话值仍然相同

感谢您的帮助。

回答

0

我的问题解决了。更改我的代码到这

  string uId = "userID"; 
      DateTime expireDate = DateTime.Now.AddDays(3); 

      var httpCookie = HttpContext.Current.Response.Cookies["cookieName"]; 
      if (httpCookie != null) 
      { 
       httpCookie["uID"] = uId; 

       HttpContext.Current.Response.Cookies.Add(httpCookie); 
      } 
      else 
      { 
       HttpCookie aCookie = new HttpCookie("cookieName"); 
       aCookie.Values["uID"] = uId; 
       aCookie.Expires = expireDate; 

       HttpContext.Current.Response.Cookies.Add(aCookie); 
      } 
0

这一个让我难住。但设法解决它如下。因此,基本上将期限设置为初始化程序的一部分不起作用。在将cookie添加到响应对象后设置它的工作原理!

enter image description here