http cookie serialization

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone give me an example of serializing an object to an http cookie and
then getting it back out? Most examples serialize via stream to a text file,
but i'd like to see the best way to get it into an out of a cookie (a small
object).

Thanks.
 
gl,

You will have to serialize to a MemoryStream. Once you have that, you
need to encode the byte stream into a string which can be passed back to the
client. The best option for this is to use base 64 (which is accompished
through the ToBase64String method on the Convert class).

However, I don't know that this is the best thing to do. After all,
cookies are not meant to be very large, and you might want to consider an
approach that google uses, which is, to have one cookie which acts as a key
for EVERYTHING, and then store everything you need on the server side.

Hope this helps.
 
Do you know anywhere that actually has an example of this? I found a sample
using a memorystream object and i figured I'd have to use that, but it didn't
have the base64 stuff with it.

Also, was that google thing you mentioned in an article somewhere? I'd be
interested in learning more about what they are doing. Thanks.

Nicholas Paldino said:
gl,

You will have to serialize to a MemoryStream. Once you have that, you
need to encode the byte stream into a string which can be passed back to the
client. The best option for this is to use base 64 (which is accompished
through the ToBase64String method on the Convert class).

However, I don't know that this is the best thing to do. After all,
cookies are not meant to be very large, and you might want to consider an
approach that google uses, which is, to have one cookie which acts as a key
for EVERYTHING, and then store everything you need on the server side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

gl said:
Can someone give me an example of serializing an object to an http cookie
and
then getting it back out? Most examples serialize via stream to a text
file,
but i'd like to see the best way to get it into an out of a cookie (a
small
object).

Thanks.
 
gl,

Once you write the contents to the memory stream, you call ToArray on
the memory stream and pass that to the ToBase64String method on the Convert
class.

As for the google thing, I don't know if there is an article out there
on it, but you could just do a search on "google cookie" and see what comes
up.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

gl said:
Do you know anywhere that actually has an example of this? I found a
sample
using a memorystream object and i figured I'd have to use that, but it
didn't
have the base64 stuff with it.

Also, was that google thing you mentioned in an article somewhere? I'd be
interested in learning more about what they are doing. Thanks.

Nicholas Paldino said:
gl,

You will have to serialize to a MemoryStream. Once you have that,
you
need to encode the byte stream into a string which can be passed back to
the
client. The best option for this is to use base 64 (which is accompished
through the ToBase64String method on the Convert class).

However, I don't know that this is the best thing to do. After all,
cookies are not meant to be very large, and you might want to consider an
approach that google uses, which is, to have one cookie which acts as a
key
for EVERYTHING, and then store everything you need on the server side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

gl said:
Can someone give me an example of serializing an object to an http
cookie
and
then getting it back out? Most examples serialize via stream to a text
file,
but i'd like to see the best way to get it into an out of a cookie (a
small
object).

Thanks.
 
Back
Top