To set value copy following method:
public static void SetCookie(string key, string value)
{
string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;
DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(2000);
string cookie = String.Format("{0}={1};expires={2}", key, value, expiration.ToString("R"));
HtmlPage.Document.SetProperty("cookie", cookie);
}
To get cookie value copy below method.
public static string GetCookie(string key)
{
try
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
//key += '=';
foreach (string cookie in cookies)
{
string cookieStr = cookie.Trim();
string[] vals = cookieStr.Split('=');
if (vals.Length >= 2)
{
if (vals[0].Equals(key, StringComparison.OrdinalIgnoreCase))
{
return vals[1];
}
}
}
}
catch
{
}
return null;
}
Delete value from cookie add below method
public static void DeleteCookie(string key)
{
string oldCookie = HtmlPage.Document.GetProperty("cookie") as String;
DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);
string cookie = String.Format("{0}=;expires={1}", key, expiration.ToString("R"));
HtmlPage.Document.SetProperty("cookie", cookie);
}