在内容页面之间遍历时 cookie 值消失
在我的应用程序中。有一个登录机制,它保存一个 cookie,其中包含刚刚登录我的主页加载的用户的信息
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 是否存在:
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),请关闭浏览器,然后运行我的应用程序。再次是饼干 存在它的值是正确的并且用户“自动”登录。
如果我首先从启动内容页面重定向到不同的内容页面 cookie 值也完好无损,
问题是当我第二次重定向回不同的内容页面时, 主页加载并进行检查 cookie 存在,但值被删除...
关于为什么会发生这种情况有什么想法吗?
顺便说一句,也许我注销的方式可能是这个问题的原因:
当我注销时,我创建了一个同名的 cookie,该 cookie 于 1 天前过期。
private void Remove_Cookie()
{
HttpCookie cookie = new HttpCookie("user");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
在 iv'e 描述的情况下,我没有正式注销,我只是结束我的应用程序,所以这不应该 有什么影响。
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 .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,这个问题是不可想象的
特别感谢 Peter Bromberg
http://www.eggheadcafe.com/tutorials/aspnet/198ce250-59da-4388-89e5-fce33d725aa7/aspnet-cookies-faq.aspx
文章“消失”部分Cookie "
作者指出,如果您关注 Response.Cookies["cookie_name"]
浏览器创建一个新的空 cookie 来覆盖您的 cookie 。
我使用了这样的手表,它使我的饼干失去了它的值,当我把它从饼干上取下时,它保留了它的值。
道德是不要看 Response.Cookies[" "]
我还在其他一些帖子中读到,如果您检查
例如它也会被覆盖。
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
for example it also gets overridden.
为了重申并建立在已经陈述的基础上(是的,我知道这是一个 4 年前的问题),我发现最好构建一个实用程序来处理这个问题 - 主要是因为我想经常检查特定的 cookie。
这不会触及响应,而只会读取请求。
经验法则
始终从请求中读取并写入响应。
谢谢 eran!这篇文章正是我所需要的
To reiterate and build upon what has already been stated (yes, I know this is a 4 year old question) I have found it best to build a utility to handle this - mostly because I want to check that specific cookie often.
This will not touch the Response but only read from the Request.
rule-of-thumb
Always read from the Request and write to the Response.
Thanks eran! this post was exactly what I needed
尝试以下操作:
try the following:
使用以下方法从 cookie 中获取值:
Use the following approach to get a value from cookies: