Cookies for Silverlight

by Jim McCurdy 31. July 2009 22:30

Here is a simple static class, Cookie, that offers methods to Read and Write cookies from Silverlight applications.  You can also specify the cookie expiration as a number of days:

  • expireDays = 0, indicates a session cookie that will not be written to disk
  • expireDays = -1, indicates that the cookie will not expire and will be permanent
  • expireDays = n, indicates that the cookie will expire in “n” days

The code below has been tested and used with Silverlight 3 and Silverlight 4.

using System;
using System.Windows.Browser;

namespace ClassLibrary
{
	public static class Cookie
	{
		public static bool Exists(string key, string value)
		{
			return HtmlPage.Document.Cookies.Contains(key + "=" + value);
		}

		public static string Read(string key)
		{
			string[] cookies = HtmlPage.Document.Cookies.Split(';');
			foreach (string cookie in cookies)
			{
				string[] keyValuePair = cookie.Split('=');
				if (keyValuePair.Length == 2 && key == keyValuePair[0].Trim())
					return keyValuePair[1].Trim();
			}

			return null;
		}

		public static void Write(string key, string value, int expireDays)
		{
			// expireDays = 0, indicates a session cookie that will not be written to disk 
			// expireDays = -1, indicates that the cookie will not expire and will be permanent
			// expireDays = n, indicates that the cookie will expire in “n” days
			string expires = "";
			if (expireDays != 0)
			{
				DateTime expireDate = (expireDays > 0 ?
				DateTime.Now + TimeSpan.FromDays(expireDays) :
				DateTime.MaxValue);
				expires = ";expires=" + expireDate.ToString("R");
			}

			string cookie = key + "=" + value + expires;
			HtmlPage.Document.SetProperty("cookie", cookie);
		}

		public static void Delete(string key)
		{
			DateTime expireDate = DateTime.Now - TimeSpan.FromDays(1); // yesterday
			string expires = ";expires=" + expireDate.ToString("R");
			string cookie = key + "=" + expires;
			HtmlPage.Document.SetProperty("cookie", cookie);
		}
	}
}

Comments (4) -

Silverlight
Silverlight
4/13/2010 6:21:03 PM #

is there a way to create a sliding cookie?

Reply

Jim McCurdy
Jim McCurdy
4/14/2010 12:54:45 AM #

I believe that sliding cookies only apply to authentication cookies.  If that is what you want, check out this article (http://forums.asp.net/t/623339.aspx), which describes how to set that option in your web.config.

Reply

מרק עדשים
מרק עדשים
6/13/2011 12:46:03 AM #

First thank you for the great resource. Only one Question: What can i do if the user disables cookies in his browser?

Reply

John Rob
John Rob
1/9/2012 3:30:30 AM #

Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about How to read and write Cookies in asp.Net, for more details of this post check out this link…

mindstick.com/.../

Thanks

Reply

Pingbacks and trackbacks (3)+

Add comment

biuquote
  • Comment
  • Preview
Loading

About Me

My Photo Jim is always looking for new clients in need of software development expertise.  He is particularly well suited for Silverlight, WPF, and ASP.NETprojects.

Professional Biography

Jim McCurdy operates Face to Face Software, where he works designing, developing, and managing software projects for a variety of clients.

Jim currently specializes in contract development projects for Silverlight, WPF, and ASP.NET platforms. Recent projects include two complete web sites developed using Silverlight, .NET, and C#. The first is YinYangMoney.com, a unique financial and lifestyle planning web application. The second is McPivot.com, a unique web application that presents custom queries and visualization of baseball data back to the 1800's.

Jim has worked in the software industry for as long as he can remember, and has broad expertise in Web, Windows, and systems technologies. Jim has been a team player on many agile development projects throughout his career, and is a founding member at software startups Astral Development and Powerhouse Entertainment.

You can Email Jim at Face to Face Software.

Blog Archive

Welcome Readers