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

This code below works with Silverlight 3.

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);
		}
	}
}
Digg It!DZone It!Del.icio.usStumbleUpon

Comments

1/29/2010 9:43:07 AM #

Cookies for Silverlight

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

2/1/2010 11:58:46 AM #

Cookies for Silverlight

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

Add comment




biuquote
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen


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

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 development projects for Silverlight, .NET, ASP.NET, and WPF platforms. One such project is a complete web site using Silverlight, .NET, and C#; a unique financial and lifestyle planning web application at YinYangMoney.com.

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.


Welcome Readers