Silverlight Get Cookie Value

By Robbe Morris

Quick tip for getting the value of a cookie in Silverlight.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Browser;

public static class DOMController
{

public static string GetCookieValue(string key)
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
key += '=';

foreach (string cookie in cookies)
{
string cookieVal = cookie.Trim();
if (!cookieVal.StartsWith(key, StringComparison.OrdinalIgnoreCase)) continue;

string[] vals = cookieVal.Split('=');
if (vals.Length >= 2) return vals[1];
}
return string.Empty;
}

}

Related FAQs

Quick tip for retrieving querystring variables in Silverlight code behind.
Silverlight Get Cookie Value  (2510 Views)