Monday, November 22, 2010

How to write and read cookies from Silverlight ?

Here is the sample code to write and read cookies from Silverlight.

Make sure the DateTime.UtcNow is used.


private void SaveCookie(string key, string value)
{
// Expire in 30 days
DateTime expireDate = DateTime.UtcNow + TimeSpan.FromDays(30);
string newCookie = key + "=" + value + ";expires=" + expireDate.ToString("R");
HtmlPage.Document.SetProperty("cookie", newCookie);
}




private string ReadCookie(string key)
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
foreach (string cookie in cookies)
{
string[] keyValue = cookie.Split('=');
if (keyValue.Length == 2)
{
if (keyValue[0].ToString().Trim() == key.Trim())
return keyValue[1];
}
}
return null;
}

How to update Internet Explorer Status Bar from Silverlight ?

Here is the sample code which checks for Internet Explorer and updates the status bar to "Hello World".

string strAgent = HtmlPage.BrowserInformation.UserAgent;
if (strAgent.IndexOf("MSIE") > -1)
    HtmlPage.Window.SetProperty("status", "Hello World");

All Blogs so far ...