Overview
A lot of friends have written about how to manipulate cookies in Silverlight 2, and here is a special article about it. In order to implement the operation of cookies in Silverlight applications, we need to use the Htmlpage.document object.
Before you use Htmlpage.document, add the System.Windows.Browser namespace. This article describes how to manipulate cookies in a Silverlight application, and at the end gives you a common class for manipulating cookies, which you can use directly in your own application.
Write Cookie
In the Silverlight application, We can set cookies by using the HtmlPage.Document.SetProperty method or use the Cookies property of the Htmlpage.document object (as we'll see later), as shown in the following code:
void btnSet_Click(object sender, RoutedEventArgs e)
{
DateTime expir = DateTime.UtcNow + TimeSpan.FromDays(7);
String cookie = String.Format("{0}={1};expires={2}",
this.txtKey.Text,
this.txtValue.Text,
expir.ToString("R"));
HtmlPage.Document.SetProperty("cookie", cookie);
}
This sets the expiration time of a cookie to one week, in addition to setting the expiration time, you can set domain, path, and so on, you will see this in the Help class later.
Use the following interface to write cookies: