Creating a Cookie and Setting it

Posted by Vuyiswamb under ASP.NET on 5/25/2011 | Points: 10 | Views : 1479 | Status : [Member] [MVP] [Administrator] | Replies : 1
Good Evening All

i have another Question. i have created a Function that creates a Cookie with a Domain like this

private static void SetCookieExtedend(String key, String value, String path)

DateTime expires = DateTime.UtcNow + TimeSpan.FromDays(2000);

bool secure = false;

String domain = "Ecash";

StringBuilder cookie = new StringBuilder();

cookie.Append(String.Concat(key, "=", value));

if (expires != null)

cookie.Append(String.Concat(";expires=", expires.ToString("R")));

if (!String.IsNullOrEmpty(path))
cookie.Append(String.Concat(";path=", path));

if (!String.IsNullOrEmpty(domain))
cookie.Append(String.Concat(";domain=", domain));

if (secure)

HtmlPage.Document.SetProperty("cookie", cookie.ToString());

and its working well , i have set the Cookie and it was stored as this

UserID=142;expires=Mon, 14 Nov 2016 16:58:19 GMT;domain=Ecash

now i want to retrieve the Value of the Cookie and i have a function that does it like this

public static string GetCookie(string key)
string[] cookies = HtmlPage.Document.Cookies.Split(';');
key += '=';
foreach (string cookie in cookies)
string cookieStr = cookie.Trim();
if (cookieStr.StartsWith(key, StringComparison.OrdinalIgnoreCase))
string[] vals = cookieStr.Split('=');

if (vals.Length >= 2)
return vals[1];

return string.Empty;

return null;

the Following Line comes back with an empty String

string[] cookies = HtmlPage.Document.Cookies.Split(';');

Why ?


Thank you for posting at Dotnetfunda


Posted by: Jayakumars on: 5/26/2011 [Member] [MVP] Bronze | Points: 25


HtmlPage namespace didnot support which namespace use this another one may be string separate didnot have this -> ;

i will try and reply asked namespace proper

Mark as Answer if its helpful to you

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response