473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Help with Cookies

I am trying to write some values to cookies to later get those values on
postback events.
I want to know how to store a multi-value cookie, and request those values.
I am using C#. Can anyone help with some code?

Hector
Nov 19 '05 #1
3 986
one possible way:

i am using an object wrapper for my values and i will serialize my
object and write it in a cookie, and then upon the retrival i will
deserialize it.
But you should be careful about the size.

Hope this helps.

Nov 19 '05 #2
Can you help me with the code?

Hector

"ar****@yahoo.com" wrote:
one possible way:

i am using an object wrapper for my values and i will serialize my
object and write it in a cookie, and then upon the retrival i will
deserialize it.
But you should be careful about the size.

Hope this helps.

Nov 19 '05 #3
Ok,
first please note I have not tested the following code, and i typed it
directly here.

Assume you have two attrubures that you wish to put them in a cookie:

string FName;
string LName
option one:

HttpCookie cookie = new HttpCookie("NameCookie");
cookie.Values["FName"] = "ffff";
cookie.Values["LName"] = "llll";
HttpResponse.Cookies.Add(cookie);

for retrieving use HasKeys.

option two:
this option is designed for masochist people like me! But seriously it
has some advantages (and i am not going to tell you what they are).

you need to create a class to wrap your types, in our case two
properties, something like:
[Serilialize]
public class Name
{
string fname, lname;

public Name(string fname, string lname)
{
this.fname = fname;
this.lname = lname;
}

public string FName
{
get { return fname; }
set { fname = value; }
}

public string LName
{
get { return lname; }
set { lname = value; }
}
}

now you have to write a serializer method that returns a string:

string SerilizeName(Name name)
{
IFormatter formatter = new BinaryFormatter();
StringWriter write= new StringWriter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, name);

//please note you should use different encoding
return Convert.ToBase64String(stream.ToArray());
}

now the main method:

void WriteToCookie()
{
Name name = new Name("fff", "lll");
HttpCookie coockie = new HttpCookie("NameCookie", SerilizeName(name))
HttpResponse.Cookies.add(coockie);
}

upon the cookie retreival you should deserilize the string and convert
it to the Name class, and i will leave the deserilizer to you ;)

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: collie | last post by:
Hi, I have an asp page that needs to create 2 different cookies: one for the admin and one for the user. The code that I have to work with was created by someone else. the page first requires to...
18
by: | last post by:
Please help. After a number of wrong turns and experiments I need advice on login management system to secure our web pages without inconveniencing our visitors or our internal staff. What I...
0
by: Bennett F. Dill | last post by:
Thanks for reading. I'm having problems with cookies from asp to asp.net and back! It seems like I can set a cookie in asp.net fine, and alter it at will, as soon as asp touches it, asp.net...
3
by: Calvin KD | last post by:
Hi everyone, Can someone tell me what's wrong with the way that i read a cookie as below: private void Page_Load(object sender, System.EventArgs e) { Response.Cookies.Clear(); HttpCookie...
4
by: serge calderara | last post by:
Dear all, I try to make some test on how to use basic cookie but get some trouble in the sens that I was not able to read back previous cretaed cookies. In page_load event I have the following...
0
by: Alan Silver | last post by:
Hello, I am having a problem setting and resetting cookies. I'm sure I just doing something really stupid as this is such a basic issue, but I can find any answer. Please can someone help me? ...
3
by: Phillip N Rounds | last post by:
I'm having trouble with using cookies to monitor the stages of login. I have a two stage Registration page ( register.aspx ) and my target page ( MyPage.aspx ) I'm using a cookie named LoginStatus...
7
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
2
by: isaac2004 | last post by:
hi i am creating a basic asp site that uses cookies to manage a cart for an online store. whenever i open this page without adding anything to the cart. i get an error message. here is my code ...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.