Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

c# - cookie values disappear when traversing between content pages

in my app. there's a log in mechanism which save a cookie with the info of the user who just logged in

     private void CreateCookie(LoginEventArgs args)
     {
         HttpCookie cookie = new HttpCookie("user");
         cookie.Values["name"] = args.User_Name;
         cookie.Values["id"] = args.ID;
         cookie.Expires = DateTime.Now.AddDays(1);            
         Response.Cookies.Add(cookie);
     }

on my master page load i perform a check to see if this cookie exists or not :

   HttpCookie cookie = Request.Cookies["user"] ;
   if( (cookie != null) && (cookie.Value != ""))  
   {
        if (Session["user"] == null)
            Login_Passed(this, new LoginEventArgs(cookie.Values["name"].ToString(), int.Parse(cookie.Values["id"])));
   }

now if i Log in ( Create A cookie ) , close the browser , and run my app. again the cookie exists it's values are correct and the user is "automatically" logged in .

if i first redirect to a different content page from the start up content page the cookies values are also intact ,

the problem is when i redirect back to a different content page a second time, the master page loads , makes the check the cookie exists but the values are deleted ...

any ideas on why this happens ?

btw maybe the way i log out could be the reason for this problem :

when i log-out i create a cookie with the same name that expires 1 day ago .

   private void Remove_Cookie()
   {
        HttpCookie cookie = new HttpCookie("user");
        cookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(cookie); 
   }

in the case iv'e described i don't log-out formally , i just end my app , so this shouldn't have any effect .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

o'k , the problem was unthinkable
special thanks to Peter Bromberg

http://www.eggheadcafe.com/tutorials/aspnet/198ce250-59da-4388-89e5-fce33d725aa7/aspnet-cookies-faq.aspx

in the section of the Article " The Disappearing Cookie "

the author states that if you have a watch on Response.Cookies["cookie_name"] the browser creates a new empty cookie that overrides your cookie .

i used such a watch which made my cookie loose it's values ,and when i took it off the cookie kept its values.

the moral is DON't WATCH Response.Cookies[" "] also i read in some other post that if you check

 if( Response.Cookies["cookie_name"] != null    )  

for example it also gets overridden.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...