Jim McCurdy's Tech Blog

Insights into Software Development and Silverlight

Cookies for Silverlight

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

    4/13/2010 6:21:03 PM |

    is there a way to create a sliding cookie?

    • 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.

  • מרק עדשים

    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?

  • 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

Comments are closed