ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

SetCookie(), GetCookie(), DeleteCookie()

Extension methods on HtmlDocument used to read, write and delete cookies in Silverlight applications.

Source

using System;
using System.Windows.Browser;

// Source: http://www.eggheadcafe.com/community/aspnet/12/10035916/retrieve-and-delete-cooki.aspx 
namespace CookiesDemo {

    public static class CookieExtensions {

        /// <summary>
        /// Sets a persistent cookie which expires after the given number of days
        /// </summary>
        /// <param name="doc">The HtmDocument to extend</param>
        /// <param name="key">the cookie key</param>
        /// <param name="value">the cookie value</param>
        /// <param name="days">The number of days before the cookie expires</param>
        public static void SetCookie(this HtmlDocument doc, string key, string value, int days) {
            DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(days);
            SetCookie(doc, key, value, expiration);
        }

        /// <summary>
        /// Sets a persistent cookie with an expiration date
        /// </summary>
        /// <param name="doc">The HtmDocument to extend</param>
        /// <param name="key">the cookie key</param>
        /// <param name="value">the cookie value</param>
        public static void SetCookie(this HtmlDocument doc, string key, string value, DateTime expiration) {
            string oldCookie = doc.GetProperty("cookie") as String;
            string cookie = String.Format("{0}={1};expires={2}", key, value, expiration.ToString("R"));
            doc.SetProperty("cookie", cookie);
        }

        /// <summary>
        /// Retrieves an existing cookie
        /// </summary>
        /// <param name="doc">The HtmDocument to extend</param>
        /// <param name="key">cookie key</param>
        /// <returns>null if the cookie does not exist, otherwise the cookie value</returns>
        public static string GetCookie(this HtmlDocument doc, string key) {
            string[] cookies = doc.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="doc">The HtmDocument to extend</param>
        /// <param name="key">the cookie key to delete</param>
        public static void DeleteCookie(this HtmlDocument doc, string key) {
            string oldCookie = doc.GetProperty("cookie") as String;
            DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);
            string cookie = String.Format("{0}=;expires={1}", key, expiration.ToString("R"));
            doc.SetProperty("cookie", cookie);
        }
    }
}

Example

HtmlPage.Document.SetCookie("key", "example", 5);

string value = HtmlPage.Document.GetCookie("key");

Author: Fons Sonnemans

Submitted on: 10 dec 2008

Language: C#

Type: System.Windows.Browser.HtmlDocument

Views: 10404