Tip: Manipulate cookies in Silverlight applications

Source: Internet
Author: User
Tags datetime silverlight

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.