Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

Cookie not memorize the right value of variable in c#

In code-behind of my aspx page I create this cookie:

cookieidUserArea = new HttpCookie("idUserArea");
cookieidUserArea.Expires = DateTime.Now.AddMinutes(15);
cookieidUserArea.Values.Add("", idUserArea.ToString());
Response.Cookies.Add(cookieidUserArea);

To display the value of the variable stored in the cookie I used:

Response.Write(Request.Cookies["idUserArea"].Value + "<br />");

The value memorized in cookie it should be :

AA40

instead I have

=AA40

the presence of the symbol = in the cookie memorized produce problems in the application in the following steps.

I have tried this Replace without success:

cookieidUserArea.Values.Add("", idUserArea.ToString().Replace("=", ""));

Anybody know how can I resolve do this?

Can you suggest?

Can you help me?

Thank you in advance.

Upvotes: 3

Views: 79

Answers (2)

Felix Av
Felix Av

Reputation: 1254

As said before if you want to store just one value you can pass it n the constructor of the cookie, something like that:

cookieidUserArea = new HttpCookie("idUserArea",idUserArea.ToString());
cookieidUserArea.Expires = DateTime.Now.AddMinutes(15);
Response.Cookies.Add(cookieidUserArea);

Upvotes: 0

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

That is because you use multi-value cookie. I suppose you want to use it like this instead:

cookieidUserArea.Value = idUserArea.ToString();

Alternatively you can retrieve the value like:

Request.Cookies["idUserArea"].Values[""]

Bottom line - don't mix single value and multi-value approach.

Upvotes: 4

Related Questions