Cookie的丑事 (2)

原创 2007年10月09日 10:52:00

今天终于弄清楚,为什么cookie总在关闭浏览器有消失了,原来还因为是弹出窗口冲突的问题。。。

都怪自己苯苯

 昨天的写法

 

if (Request.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID] == null)
                    
...{
                        Response.Cookies.Add(
new HttpCookie(Cookie.CCookieManage.COOKIE_TEMPUSERID));
                    }

                    Response.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID].Domain 
= "buildbook.com.cn";

                  
//错误就在这里,不能这样直接设置时间
                    Response.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID].Expires = DateTime.Now.AddDays (10);         //延长十天有效

                    UserID 
= Request.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID].Value;
                    
//如果cookie不存在userid,生成临时用户名
                    if (string.IsNullOrEmpty(UserID))
                    
...{
                        CMemberRule MemberRule 
= new CMemberRule();
                        UserID 
= MemberRule.GenerateTempUserName();
                        Response.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID].Value 
= UserID;
                    }


 

修改后写法

 

 //添加cookie
                   HttpCookie TempUserCookie = new HttpCookie(Cookie.CCookieManage.COOKIE_TEMPUSERID);
                   TempUserCookie.Domain 
= "buildbook.com.cn";
                   TempUserCookie.Expires 
= DateTime.Now.AddDays(10);
                     
                   
if (Request.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID] != null)
                   
...{
                       UserID 
= Request.Cookies[Cookie.CCookieManage.COOKIE_TEMPUSERID].Value;
                       Response.Cookies.Remove(Cookie.CCookieManage.COOKIE_TEMPUSERID);
                   }

                   
//如果userid不存在,生成临时用户名
                   if (string.IsNullOrEmpty(UserID))
                   
...{
                       CMemberRule MemberRule 
= new CMemberRule();
                       UserID 
= MemberRule.GenerateTempUserName();
                   }

                   TempUserCookie.Value 
= UserID;

                   
//添加cookie
                   TempUserCookie.Value = UserID;
                   Response.Cookies.Add(TempUserCookie);
收藏助手
不良信息举报
您举报文章:Cookie的丑事 (2)
举报原因:
原因补充:

(最多只允许输入30个字)