1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Setting A Cookie in ASP.NET

Discussion in 'ASP.NET' started by pradeep, Oct 18, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

    What Is A Cookie



    A cookie is a small text file sent to a web user's computer by a website. A cookie can be used to identify that user to the website on their next visit. Common uses include remembering login data, user preferences, and providing favorites lists.

    How Does A Cookie Work



    HTTP cookies are used by Web servers to differentiate users and to maintain data related to the user during navigation, possibly across multiple visits. When an user visits/logs in to go4expert.com the web server sets a cookie to track the user's last visted time, and some ID to track the specific visitor. Many websites also use cookies for personalization based on users' preferences.

    How Much Can We Store In A Cookie



    Cookie specifications suggest that browsers should support a minimal number of cookies or amount of memory for storing them. In particular, an internet browser is expected to be able to store at least 300 cookies of four kilobytes each (both name and value count towards this 4 kilobyte limit), and at least 20 cookies per server or domain, so it is not a good idea to use a different cookie for each variable that has to be saved. It's better to save all needed data into one single cookie.

    Setting A Cookie in ASP.Net



    Setting cookies with a ASP.Net script is easy, we just need to use the HttpCookie class in the System.Web namespace. Let's look at an example on creating a cookie with ASP.Net

    Code:
     //Creating the cookie 
     HttpCookie sampleCookie = new HttpCookie("User");
     sampleCookie.Values.Add("Name", strUserName);
     sampleCookie.Values.Add("CountryCode", strCountry);
     sampleCookie.Expires = #01/01/2009#; // set the expiry
     Response.Cookies.Add(sampleCookie); // Add it to the header
     
     //Getting a cookie value
     String sGetCookie;
     strUserName = Request.Cookies("User")("Name").ToString();
     strUserCountry = Request.Cookies("User")("CountryCode").ToString();
     

    References



    http://www.joedolson.com/glossary.php
    http://en.wikipedia.org/wiki/HTTP_cookie
     
  2. JGRobinson

    JGRobinson New Member

    Joined:
    Nov 15, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Home Page:
    Excellent and very clear article - The only thing which I would add is that by default cookies are only sent for the domain which created them

    Therefore if the cookie was written by test.go4expert.com it could not be read by forums.go4expert.comThe way of fixing this is to set the domain of the cookie to the base domain, then both forums.go4expert.com and test.go4expert.com could access the information

    The code to add would be :-

    Code:
    Response.Cookies("domain").Value = DateTime.Now.ToString()
    Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
    Response.Cookies("domain").Domain = "go4expert.com"
    
    I've tripped over this before and thought it was worth clarifyng.
     
  3. zaanrider

    zaanrider New Member

    Joined:
    Nov 19, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Very nice artical..thanks
     

Share This Page