Getting, Setting , Deleting and Updating Cookie Data as Collection in ASP.Net

Here is a helper class that makes it easy to do all the data manipulation functions on the data stored in the Cookie. It makes use of HybridDictionary Collection object and is very efficient for handling data in the Cookie.

  Here is How you Use it:

             (To Save a Cookie)

   Cookies EmployeeCookie = new Cookies ("EmployeeCookie" );
   EmployeeCookie.SetValue("Employee_NO", myProperty1);
   EmployeeCookie.SetValue("NAME",myProperty2);
   EmployeeCookie.Save();

  (To Delete a Cookie)

   Cookies EmployeeCookie = new Cookies ("EmployeeCookie");
   EmployeeCookie.Delete();

(Getting values from Cookie)  

 Cookies EmployeeCookie = new Cookies ("EmployeeCookie" );
   EmployeeCookie.GetCookie();
   StudentNo = EmployeeCookie.GetValue("Employee_NO");
   myProperty1 = EmployeeCookie.GetValue("NAME");

Just Add the following Class in to you Project or Class library and feel free to add your own functions as well.

HELPER CLASS:

using System;
using System.Web ;
using System.Collections ;
using System.Collections.Specialized;
using System.Web.Security;

namespace Jawad.Utils.Web
{
 ///

 /// Summary description for Cookies.
 ///
 public class Cookies
 {
  private HttpContext ctx = HttpContext.Current ;
  private string _cookieName = null;
  private HybridDictionary _data;

  public HybridDictionary CookieData
  {
   get
   {
    if (CookieData == null)
    {
     this.GetCookie ();
    }
    return _data;
   }
   set
   {
    _data = value;
   }
  }

  public void SetValue(string Key, string Value)
  {
   if ( _data == null)
    _data = new HybridDictionary ();
   _data.Add (Key, Value);
  }

  public string GetValue(string Key)
  {
   string retValue = string.Empty;

   if ( _data != null)
   {
    retValue = _data[Key].ToString ();
   }
   return retValue;
  }
  
  private Cookies()
  {
  }

  public Cookies(string cookieName)
  {
   _cookieName = cookieName;
  }

  public void Save()
  {
   // Setting a cookie's value and/or subvalue using the HttpCookie class
   HttpCookie cookie;

   if(ctx.Request.Cookies[_cookieName] != null)
    ctx.Request.Cookies.Remove(_cookieName);
   cookie = new HttpCookie(_cookieName);
   if ( _data.Count > 0 )
   {
    IEnumerator cookieData = _data.GetEnumerator();
    DictionaryEntry item;
    while ( cookieData.MoveNext() )
    {
     item = (DictionaryEntry)cookieData.Current ;
     cookie.Values.Add (item.Key.ToString (), item.Value.ToString ());
    }
   }
    ctx.Response.AppendCookie(cookie);
  }

  public void GetCookie()
  {
   // Retrieving a cookie's value(s)
   if(ctx.Request.Cookies[_cookieName] != null)
   {
    NameValueCollection values = ctx.Request.Cookies[_cookieName].Values;
    if ( values.Count > 0 )
    {
     _data = new HybridDictionary (values.Count);
     foreach(string key in values.Keys)
     {
      _data.Add (key,values[key]);
     }
    }
   }
  }

  public void Delete()
  {
   // Set the value of the cookie to null and
   // set its expiration to some time in the past
   if ( ctx.Response.Cookies[_cookieName] != null)
   {
    ctx.Response.Cookies[_cookieName].Value = null;
    ctx.Response.Cookies[_cookieName].Expires =
     System.DateTime.Now.AddMonths(-1); // last month
   }
  }
 }
}

Use Code Conversion Utilities posted in my earlier Post to convert it to Visual Basic.Net