Wednesday, May 9, 2007

System.Web.HttpCookie vs System.Net.Cookie

You have two cookie objects in one framework and you would think that the two were related in some way shape or form.

You would even imagine that one was the base class of another or they implemented a common interface. But they don't. Normally it wouldn't be a problem because one can just set the properties of one to the other and they just work.

Except for one case. That case is when the System.Web.HttpCookie contains a collection of values. System.Net.Cookie doesn't have intrinsic support for a collection of values.

The error message that .Net gives is when you try to assign a System.Net.Cookie's value property to something that has illegal characters. The characters are ',', ';', and ' '. Those characters have to be encoded. The real awesome part is that System.Web.HttpCookie's value property returns a value with commas in it when it has a collection of values.

Fun stuff.

I got around it by making sure to URL encode those three values. It's kinda weird to me that the URL encoding doesn't happen automatically so it's transparent to the developer or that there is an easy way of converting System.Net.Cookie objects to System.Web.HttpCookie objects.

It's just interesting, to say the least.

No comments: