Developer Geeks Home

Working with Cookies in ASP.NET

RSS
 

  • Posted one year ago
  • Category: ASP.NET 4.0,
  • Audiences: Architect, Developer, System Analyst,

This article will briefly introduce you to the Cookies in ASP.NET and how work with Cookies.

What is Cookies?

The Cookie is a collection of key/value pairs that hold text values set by a page in a web request. By default, the collection is empty. The cookie contains information the Web application can read whenever the user visits the website.

Following are few examples of how to work with Cookie collections in ASP .Net web applications.

How to create a Cookie?

private void WriteCookie() 
{
  System.Web.HttpCookie aCookie = null;
  aCookie = New System.Web.HttpCookie("key","value");
  aCookie.Expires = DateTime.Now.AddDays(1); //expire in a day
  Response.Cookies.Add(aCookie);
}

How to test whether a Cookie key exists?

private bool IsKeyExists(String aKey) 
{
  HttpCookieCollection cookies = Request.Cookies;
  System.Collections.IEnumerator e = cookies.GetEnumerator();
  while (e.MoveNext()) 
  {
    if (e.Current.Equals(aKey)) 
    {
      return(true);
    }
  }
  return false;
}

You can also retrieve the key values with the AllKeys property, which returns a String array containing all the keys.

private bool IsKeyExists (String aKey) 
{
  int i;
  String[] keys;
  keys = Request.Cookies.AllKeys;
  for (i = 0; i < keys.Length; i++) 
  {
    if (keys[i] == aKey)
    {
       return(true);
    }
  }
  return(false);
}

How to read a Cookie value?

private void ReadCookie() 
{
  if(Request.Cookies["name"] != null)
  {
    HttpCookie aCookie = Request.Cookies["name"];
    Response.Write(Server.HtmlEncode(aCookie.Value));
  }
}

//Or, alternatively...

private void ReadCookie() 
{
  System.Text.StringBuilder output = new System.Text.StringBuilder();
  HttpCookie aCookie;
  for(int i=0; i<Request.Cookies.Count; i++)
  {
    aCookie = Request.Cookies[i];
    Response.Write("Cookie name = " + Server.HtmlEncode(aCookie.Name));
    Response.Write("Cookie value = " + Server.HtmlEncode(aCookie.Value));
  }
}

How to read a Cookie that contains multiple values?

private void ReadCookie() 
{
  HttpCookie aCookie;
  int i;
  String[] keys;
  HttpCookieCollection cookies = Request.Cookies;
  aCookie = cookies.Get("Items");
  if (aCookie.HasKeys) 
  {
    keys = aCookie.Values.AllKeys;
  for (i = 0; i < keys.Length; i++) 
  {
     Response.Write(keys[i] + "=" + aCookie.Values[i]);
   }
  }
  else 
  {
     Response.Write("The cookie 'Items' has no keys.");
  }
  Response.Write("Cookie value=" + aCookie.Value);
}

How to delete a Cookie?

private void DeleteCookie()
{
  HttpCookie aCookie;
  string cookieName;
  int limit = Request.Cookies.Count;
  for (int i=0; i<limit; i++)
  {
      cookieName = Request.Cookies[i].Name;
      aCookie = new HttpCookie(cookieName);
      aCookie.Expires = DateTime.Now.AddDays(-1);
      Response.Cookies.Add(aCookie);
  }
}
comments powered by Developer Geeks

My Recent Articles [130]