silverlight设置浏览器Cookies

 


silverlight小技巧
silverlight设置浏览器Cookies
  1. 设置Cookies
    • 设置Cookies需要用到SetProperty()方法。而Cookies也要格式化为“Key=Value;expires=ExpireDate”形式。
    • private void SetCookie(string key, string value)
      {   
          // 7天过期   
          DateTime expireDate = DateTime.Now + TimeSpan.FromDays(7);
          string newCookie = key + "=" + value + ";expires=" + expireDate.ToString("R");
          HtmlPage.Document.SetProperty("cookie", newCookie);
      }

  2. 获取Cookies
    • 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)
                  {
                      return keyValue[1];
                  }
              }
          }
          return null;
      }


posted @ 2008-09-02 11:56  王喆(nasa)  阅读(534)  评论(0编辑  收藏  举报