Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 150, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

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

 

Print | posted on Wednesday, May 18, 2005 10:49 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

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

this helper class help me alot in manipulating data in cookies..!!!
cheers!!
7/24/2007 2:43 AM | ryan0223
Gravatar

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

Great helper class for cookie data manip., just what I needed..time saved and great clear coding , thanks !
5/13/2008 12:37 AM | Anders
Gravatar

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

thanks for the help!
6/17/2008 4:48 PM | jon parker
Gravatar

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

Awesome Code Snippet... Great Helper Class... A++..
Thanks for the post!
5/19/2009 10:20 AM | dronis
Gravatar

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

i need the cookies add to my labtop.
11/20/2009 9:47 PM | Crystal Carter
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: