Mmmm ... Cookies


To be honest, I didn't used really cookies on a project since a very long time, but in Scoilnet, the education portal I am building for the Irish government,
here we go, I need it.
I had to implement a neat feature to let users, mostly teachers, keeping in a 'clipboard' some resources(HTML documents) for a certain period of time.

And indeed retrieve the content of their clipboard when they need it, today or in 10 days. Add to these requirements that they don't login to access the site, and., correct me if I am wrong, cookies ready to cook !

I remember vaguely that cookies implementation before was a piece of cake (sorry I couldn't missed this one;-)), so because .Net rocks, it would be easy.
Not so easy, indeed. It took me few hours to figure out how to make this working properly.

I discovered that when you write some values to a cookie, you need to check if the cookie exist to avoid an exception error.

Great, but when you write something like if request.cookies("Mycookie") is nothing then... you enter in a mysterious world.

To be more explicit, you have with .Net 2 types of cookies, the session one, and the persistent one. The first disappear when the user do the same, and the second stay on the user disk. The difference is just a tiny property Expires. If you pass to this an expiration date, it's persistent, otherwise not.

The problem, is when you do a test to check that the cookie is there, .Net for a reason which elude me, create the cookie, but with a null expiration date.

Yes you just guessed it, it's a session cookie. And now everything you can write in your cookie after that are not written to the disk, believe me I spend sometime to discover this.

In some scenario, it could be worse, if you use a watch in VS.Net, you create again this fugitive cookie.
The incoming (Request) cookie carries an Expires date of DateTime.MinValue, regardless of the date attached to the cookie on the client system.

Read this clever article on CodeProject to know more about this problem and a solution.

So my little solution is this one, I admit unclean, but unfortunatly not too much time. I am sure someone has a better explanation, but cookies and .Net are not the best ingredients for a good recipe.

' MyNewValue contain the name of the file the user want to add to the clipboard

Try

'// I use HasKeys to check that the cookie has some subvalues

If Request.Cookies("MyCookie").HasKeys then

Dim Myfilename as string = Request.Cookies("Mycookie")("filename").toString
Response.Cookies("MyCookie").Value = "clip"
Response.Cookies("MyCookie").Expires = DateTime.Now.AddDays(45)
Response.Cookies("MyCookie").Values.Add("filename", MyFileName & "@@" & MyNewValue)

Else

Strangely I was hoping that HasKeys will throw an exception if the cookie doesn't exist that's why I used the Try Catch, but sometime it does,
sometime not, and I am not yet sure why. This is why I add this Else repeating the same code. Unclean but it works !

Response.Cookies("MyCookie").Value = "clip"
Response.Cookies("MyCookie").Expires = DateTime.Now.AddDays(45)
Response.Cookies("MyCookie").Values.Add("filename", MyFileName)

End if

Catch ex as Exception

Response.Cookies("MyCookie").Value = "clip"
Response.Cookies("MyCookie").Expires = DateTime.Now.AddDays(45)
Response.Cookies("MyCookie").Values.Add("filename", MyFileName)

End Try


To read the cookie values, I just have to split the value from the cookie and write the result by a label for example:

Dim MyFileList as string() = Request.Cookies("Mycookie")("filename").Split("@@")

No Comments