silverlight设置浏览器Cookies
silverlight小技巧
- 设置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);
}
- 获取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;
}