How to use cookies in Silverlight

Source: Internet
Author: User

Cookie is a frequently used storage method in browsers. It is easy to read and maintain. May someone want to use cookies in Silverlight? The answer is inevitable! Next, let me introduce how to use cookies in SL ~

Creating a cookie also uses our old friend htmlpage.doc ument;

 

  /**/ ///   <Summary>
/// Create cookie
///   </Summary>
///   <Param name = "key"> </param>
///   <Param name = "value"> </param>
Private   Void Setcookie ( String Key, String Value)
{
Datetime expiredate = Datetime. Now + Timespan. fromdays ( 7 ); // Valid for one week
String Newcookie = Key +   " = "   + Value +   " ; Expires = "   + Expiredate. tostring ( " R " );
Htmlpage. Document. setproperty ( " Cookie " , Newcookie );
}

Reading cookies is also simple.

 

  /**/ ///   <Summary>
/// Read cookie
///   </Summary>
///   <Param name = "key"> </param>
///   <Returns> </returns>
Private   String Getcookie ( 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 () = Key)
{
ReturnKeyValue [1];
}
}
}   Return   Null ;
}

 

Source code:Set browser cookie

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.