内容页面之间遍历时,cookie值消失 [英] cookie values disappear when traversing between content pages

查看:120
本文介绍了内容页面之间遍历时,cookie值消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序。有一个日志机制,保存一个cookie与谁刚登录的用户的信息

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);
     }

这是我的主人页面加载我进行检查,看看这个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"])));
   }

现在,如果我登录(创建一个cookie),关闭浏览器,并运行我的应用程序。再次饼干
它存在的价值是正确的,用户是自动登录。

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 ,

问题是,当我重​​定向到一个不同的内容页第二次,
母版页加载,使得检查
cookie的存在,但价值被删除...

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 :

当我注销我创建使用有效期1天前相同名字的cookie。

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); 
   }

在iv'e所描述的情况,我不注销正式,我刚刚结束我的应用程序,所以这不应该
有任何影响。

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

推荐答案

o'k,问题是不可想象的结果
特别感谢彼得·布朗伯格

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

<一个href=\"http://www.eggheadcafe.com/tutorials/aspnet/198ce250-59da-4388-89e5-fce33d725aa7/aspnet-cookies-faq.aspx\">http://www.eggheadcafe.com/tutorials/aspnet/198ce250-59da-4388-89e5-fce33d725aa7/aspnet-cookies-faq.aspx

在文章消失的曲奇的栏目

in the section of the Article " The Disappearing Cookie "

撰文指出,如果你有Response.Cookies手表[cookie_name]
浏览器创建一个覆盖您的cookie一个新的空的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 .

我用这样使我的cookie松动是价值的手表,当我把它关闭了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.

寓意是不看Response.Cookies []
此外,我在其他一些阅读后,如果您检查

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.

这篇关于内容页面之间遍历时,cookie值消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆