Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...