更改已经存在的cookie的cookie值。

时间:2021-12-06 10:42:15

I have a cookie called SurveyCookie. Created like so:

我有一块饼干叫SurveyCookie。创建如下所示:

var cookie = new HttpCookie("SurveyCookie");
cookie.Values["surveyPage"] = "1";
cookie.Values["surveyId"] = "1";
cookie.Values["surveyTitle"] = "Definietly not an NSA Survey....";
cookie.Values["lastVisit"] = DateTime.UtcNow.ToString();
cookie.Expires = DateTime.UtcNow.AddDays(30);
Response.Cookies.Add(cookie);

Which works great. Now the problem comes when I want to change the value "surveyPage" like so.

工作的很好。现在,当我想像这样更改值“surveyPage”时,问题就出现了。

The below will create a new cookie which is not what I want.

下面将创建一个新的cookie,这不是我想要的。

int cookieValue = Convert.ToInt32(Request.Cookies["SurveyCookie"]["surveyPage"]) + 1;
Response.Cookies["SurveyCookie"]["surveyPage"] = cookieValue.ToString();

Then I tried this code below which doesn't work either. The surveyPage is still 1 when it should be 2.

然后我尝试了下面的代码,它也不工作。测量页面仍然是1,而应该是2。

Request.Cookies["SurveyCookie"]["surveyPage"] = cookieValue.ToString(); 

Since neither of the above works what does change the cookies value for surveyPage?

由于上述两种方法都不起作用,那么surveyPage的cookie值有什么变化呢?

2 个解决方案

#1


31  

From ASP.NET Cookies Overview:

从ASP。净饼干概述:

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.

不能直接修改cookie。相反,更改cookie的过程包括创建一个带有新值的新cookie,然后将该cookie发送到浏览器以覆盖客户机上的旧版本。

You can try this:

你可以试试这个:

HttpCookie cookie = Request.Cookies["SurveyCookie"];
if (cookie == null)
{
    // no cookie found, create it
    cookie = new HttpCookie("SurveyCookie");
    cookie.Values["surveyPage"] = "1";
    cookie.Values["surveyId"] = "1";
    cookie.Values["surveyTitle"] = "Definietly not an NSA Survey....";
    cookie.Values["lastVisit"] = DateTime.UtcNow.ToString();
}
else
{
    // update the cookie values
    int newSurveyPage = int.Parse(cookie.Values["surveyPage"]) + 1;
    cookie.Values["surveyPage"] = newSurveyPage.ToString();
}

// update the expiration timestamp
cookie.Expires = DateTime.UtcNow.AddDays(30);

// overwrite the cookie
Response.Cookies.Add(cookie);

#2


-1  

Check out the Response.SetCookie() method as this will set update your existing cookie

检查Response.SetCookie()方法,因为它将设置更新现有的cookie

#1


31  

From ASP.NET Cookies Overview:

从ASP。净饼干概述:

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.

不能直接修改cookie。相反,更改cookie的过程包括创建一个带有新值的新cookie,然后将该cookie发送到浏览器以覆盖客户机上的旧版本。

You can try this:

你可以试试这个:

HttpCookie cookie = Request.Cookies["SurveyCookie"];
if (cookie == null)
{
    // no cookie found, create it
    cookie = new HttpCookie("SurveyCookie");
    cookie.Values["surveyPage"] = "1";
    cookie.Values["surveyId"] = "1";
    cookie.Values["surveyTitle"] = "Definietly not an NSA Survey....";
    cookie.Values["lastVisit"] = DateTime.UtcNow.ToString();
}
else
{
    // update the cookie values
    int newSurveyPage = int.Parse(cookie.Values["surveyPage"]) + 1;
    cookie.Values["surveyPage"] = newSurveyPage.ToString();
}

// update the expiration timestamp
cookie.Expires = DateTime.UtcNow.AddDays(30);

// overwrite the cookie
Response.Cookies.Add(cookie);

#2


-1  

Check out the Response.SetCookie() method as this will set update your existing cookie

检查Response.SetCookie()方法,因为它将设置更新现有的cookie