技巧:在Silverlight应用程序中操做Cookie

版权声明:原创做品,容许转载,转载时请务必以超连接形式标明文章 原始出处 、做者信息和本声明。不然将追究法律责任。 http://terrylee.blog.51cto.com/342737/89836
本文首发于 博客园,做者为 TerryLee,原文地址: 技巧:在Silverlight应用程序中操做Cookie

概述

不少朋友来信问如何在Silverlight 2中操做Cookie,这里专门写篇文章介绍一下。为了实如今Silverlight应用程序中对于Cookie的操做,咱们须要借助于HtmlPage.Document对象。
在使用HtmlPage.Document以前,请先添加System.Windows.Browser命名空间。本文介绍了如何在Silverlight应用程序中操做Cookie,并在最后给出了一个操做Cookie的公用类,你们能够直接在本身的应用程序中使用。

写入Cookie

在Silverlight 应用程序中,咱们能够经过HtmlPage.Document.SetProperty方法来设置Cookie或者使用HtmlPage.Document对象的Cookies属性(后面会讲到),以下代码所示:
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);
            }
这里设置Cookie的过时时间为一周,除了设置过时时间外还能够设置domain、path等,后面的帮助类中你将看到这一点。
如使用下面的界面写入Cookie:
 

读取Cookie

咱们能够经过HtmlPage.Document.GetProperty方法来获取全部Cookie,另外在HtmlDocument中定义了Cookies属性,已经为咱们封装好了GetProperty方法,能够直接使用,它的定义以下代码所示:
public sealed class HtmlDocument : HtmlObject
            {
            public string Cookies
            {
            get{
            HtmlPage.VerifyThread();
            String property = this.GetProperty("cookie") as String;
            if (property != null)
            {
            return property;
            }
            return String.Empty;
            }
            set{
            HtmlPage.VerifyThread();
            String str = value;
            if (String.IsNullOrEmpty(str))
            {
            str = string.Empty;
            }
            this.SetProperty("cookie", str);
            }
            }
            }
如使用下面这段代码来获取一个指定Key的Cookie值:
void btnRetrieve_Click(object sender, RoutedEventArgs e)
            {
            String[] cookies = HtmlPage.Document.Cookies.Split(';');
            foreach (String cookie in cookies)
            {
            String[] keyValues = cookie.Split('=');
            if (keyValues.Length == 2)
            {
            if (keyValues[0].Trim() == this.txtKey.Text.Trim())
            {
            this.txtValue.Text = keyValues[1];
            }
            }
            }
            }
以下图所示:

删除Cookie

删除Cookie很是简单,清空Cookie的值并设置它的过时时间,以下代码所示:
void btnDelete_Click(object sender, RoutedEventArgs e)
            {
            DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);
            string cookie = String.Format("{0}=;expires={1}",
            this.txtKey.Text, expir.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", cookie);
            }

Cookie帮助类

因为在开发中,咱们可能会常常用到对于Cookie的操做,我在这里总结了一个简单的Silverlight中操做Cookie帮助类,你们能够直接在本身的项目中使用,主要有以下几个功能:
1.写入Cookie
2.读取Cookie
3.删除Cookie
4.判断Cookie是否存在
固然若是你还有别的需求,能够再进一步完善,完整的代码以下:
public class CookiesUtils
            {
            public static void SetCookie(String key, String value)
            {
            SetCookie(key, value, null, null, null, false);
            }
            public static void SetCookie(String key, String value, TimeSpan expires)
            {
            SetCookie(key, value, expires, null, null, false);
            }
            public static void SetCookie(String key, String value, TimeSpan? expires,
            String path, String domain, bool secure)
            {
            StringBuilder cookie = new StringBuilder();
            cookie.Append(String.Concat(key, "=", value));
            if (expires.HasValue)
            {
            DateTime expire = DateTime.UtcNow + expires.Value;
            cookie.Append(String.Concat(";expires=", expire.ToString("R")));
            }
            if (!String.IsNullOrEmpty(path))
            {
            cookie.Append(String.Concat(";path=", path));
            }
            if (!String.IsNullOrEmpty(domain))
            {
            cookie.Append(String.Concat(";domain=", domain));
            }
            if (secure)
            {
            cookie.Append(";secure");
            }
            HtmlPage.Document.SetProperty("cookie", cookie.ToString());
            }
            public static string GetCookie(String key)
            {
            String[] cookies = HtmlPage.Document.Cookies.Split(';');
            String result = (from c in cookies
            let keyValues = c.Split('=')
            where keyValues.Length == 2 && keyValues[0].Trim() == key.Trim()
            select keyValues[1]).FirstOrDefault();
            return result;
            }
            public static void DeleteCookie(String key)
            {
            DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);
            string cookie = String.Format("{0}=;expires={1}",
            key, expir.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", cookie);
            }
            public static bool Exists(String key, String value)
            {
            return HtmlPage.Document.Cookies.Contains(String.Format("{0}={1}", key, value));
            }
            }

总结

本文介绍了在Silverlight应用程序中如何操做Cookie,但愿对你们有所帮助。
本文示例代码以及CookiesUtils代码下载:
更多Silverlight 2的文章请参考 Silverlight 2 相关文章汇总
本文首发于 博客园,做者为 TerryLee,原文地址: 技巧:在Silverlight应用程序中操做Cookie

本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/89836html

转载于:https://www.cnblogs.com/hdjjun/archive/2008/12/24/1361583.htmlcookie