Welcome to MSDN Blogs Sign in | Join | Help

You had me at "Hello World!"

XAML, WPF, Silverlight, .NET, Office 2007, Windows code samples and other interesting things
Setting Cookies in Silverlight

Here is a simple way to set, retrieve and delete cookies from within a Silverlight application.

Click here to open below source code (Cookies.cs)

 

/// <summary>

/// sets a persistent cookie with huge expiration date

/// </summary>

/// <param name="key">the cookie key</param>

/// <param name="value">the cookie value</param>

private static void SetCookie(string key, string value)

{

    string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;

    DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(2000);

    string cookie = String.Format("{0}={1};expires={2}", key, value, expiration.ToString("R"));

    HtmlPage.Document.SetProperty("cookie", cookie);

}

 

/// <summary>

/// Retrieves an existing cookie

/// </summary>

/// <param name="key">cookie key</param>

/// <returns>null if the cookie does not exist, otherwise the cookie value</returns>

private static string GetCookie(string key)

{

    string[] cookies = HtmlPage.Document.Cookies.Split(';');

    key += '=';

    foreach (string cookie in cookies)

    {

        string cookieStr = cookie.Trim();

        if (cookieStr.StartsWith(key, StringComparison.OrdinalIgnoreCase))

        {

            string[] vals = cookieStr.Split('=');

 

            if (vals.Length >= 2)

            {

                return vals[1];

            }

 

            return string.Empty;

        }

    }

 

    return null;

}

 

/// <summary>

/// Deletes a specified cookie by setting its value to empty and expiration to -1 days

/// </summary>

/// <param name="key">the cookie key to delete</param>

private static void DeleteCookie(string key)

{

    string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;

    DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);

    string cookie = String.Format("{0}=;expires={1}", key, expiration.ToString("R"));

    HtmlPage.Document.SetProperty("cookie", cookie);

}

 

For more cookie properties (such as restrict access to a certain path/domain), see http://msdn2.microsoft.com/en-us/library/ms533693(VS.85).aspx

Published Monday, April 14, 2008 10:32 AM by nikola

Filed under: , ,

Attachment(s): Cookies.cs

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Microsoft news and tips &raquo; Setting Cookies in Silverlight @ Monday, April 14, 2008 3:05 PM

PingBack from http://microsoftnews.askpcdoc.com/?p=2344

Microsoft news and tips &raquo; Setting Cookies in Silverlight

# re: Setting Cookies in Silverlight @ Monday, April 14, 2008 4:35 PM

More info (with code) in the Silverlight forums, http://silverlight.net/forums/p/11969/38854.aspx#38854

jackbond

Leave a Comment

(required) 
required 
(required) 
Page view tracker