Integrate with the browser cookie for saving and accessing data in the cookie.
Get and Set Cookie:
// sample usage
CookieManager.SetCookie("key", "value", TimeSpan.FromSeconds(3600));
string value = CookieManager.GetCookie("value");
// cookie manager class
public class CookieManager
{
public static void SetCookie(string key, string val, TimeSpan? expires)
{
SetCookie(key, val, expires, null, null, false);
}
public static void SetCookie(string key, string val, TimeSpan? expires, string path, string domain, bool secure)
{
StringBuilder fullCookie = new StringBuilder();
fullCookie.Append(string.Concat(key, "=", val));
if (expires.HasValue)
{
DateTime expire = DateTime.UtcNow + expires.Value;
fullCookie.Append(string.Concat(";expires=", expire.ToString("R")));
}
if (path != null)
{
fullCookie.Append(string.Concat(";path=", path));
}
if (domain != null)
{
fullCookie.Append(string.Concat(";domain=", domain));
}
if (secure)
{
fullCookie.Append(";secure");
}
HtmlPage.Document.SetProperty("cookie", fullCookie.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));
}
}
Website: http://www.shinedraw.com





