Cookie未更新 [英] Cookie not updating

查看:134
本文介绍了Cookie未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新cookie值,但是它不起作用-我尝试过的所有操作都不会更新cookie,并且我总是会得到cookie的初始值。

I am trying to update a cookie value, but it doesn't work - everything I have tried doesn't update the cookie and I always get the initial value of the cookie.

因此我进行了搜索,根据MSDN,我应该能够通过以下操作来更新cookie:

So I have searched and according to MSDN I should be able to update a cookie by doing the following:

HttpCookie cookie = new HttpCookie("cookiename");
cookie.Value = cookieValue;
cookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Set(cookie); // also just tried using .Add again here

由于此操作无效,因此我进行了另一次搜索SO上的人说我应该可以做到:

As this didn't work, I did another search and people on SO said I should be able to do this:

HttpContext.Current.Response.Cookies["cookiename"].Value = cookieValue;
HttpContext.Current.Response.Cookies["cookiename"].Expires = DateTime.Now.AddDays(30);

但这都不起作用,所以我尝试删除cookie并重新添加它:

But this didn't work either so I tried deleting the cookie and re-adding it:

HttpCookie cookie = new HttpCookie("cookiename");
cookie.Value = cookieValue;
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(cookie);

cookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(cookie);

在重新添加Cookie之前,我也曾尝试使用以下方法删除Cookie

I have also tried removing the cookie with the following before re-adding it

ResponseCookies.Remove("cookiename");

这也行不通,所以我现在不知道可以尝试什么。有人知道如何使用c#更新Cookie吗?

And this also didn't work, so I'm now not sure what else to try. Does anyone know how to update a cookie with c#?

更新

如果更新代码后,我检查了 HttpContext.Current.Request.Cookies [ cookiename]。Value ,它显示了新值。如果然后我回收应用程序池,以便必须再次从文件中读取cookie,它将显示原始值,因此上述代码似乎并未更新物理cookie

If I step through the code and check HttpContext.Current.Request.Cookies["cookiename"].Value after I have updated it, it shows the new value. If I then recycle the app pool so that the cookie has to be read from the file again, it shows the original value, so it seems the above code is not updating the physical cookie

推荐答案

您不能!

根据 MSDN ,您已用一个具有相同名称的新cookie替换了当前cookie。有一整节内容。

According to MSDN, you have replace the current cookie with a new one with the same name. There is a whole section about this.


修改和删除Cookies

您不能直接修改Cookie。相反,更改cookie包括使用新值创建新cookie
,然后将cookie发送到浏览器到
,以覆盖客户端上的旧版本。

You cannot directly modify a cookie. Instead, changing a cookie consists of creating a new cookie with new values and then sending the cookie to the browser to overwrite the old version on the client.

更新

在写完评论后,我们在这里找到了问题。

After writing in the comments, we found the problem here.

根据规范


此字符串是由不包括分号组成的一系列字符,逗号
空白。如果需要在名称或
值中放置此类数据,则建议使用
这样的编码方法,例如URL样式%XX编码,尽管未定义或不需要编码。

This string is a sequence of characters excluding semi-colon, comma and white space. If there is a need to place such data in the name or value, some encoding method such as URL style %XX encoding is recommended, though no encoding is defined or required.

这篇关于Cookie未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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