Wednesday, 23 April 2014

What is Cookie and how many types of Cookies in Asp.Net

 Cookie: Cookie is a small amount of memory used by web server within client system.

  •  The main purpose is maintaining personal information of client within Client system to reduce burden  on server.
   cookie can be classified into two types
  1.   In-Memory Cookie
  2. Persistent Cookie        
 In-Memory Cookie : The Cookie placed within browser process memory is called In-Memory Cookie
  • It is temporary Cookie specific to browser instance. Once browser will be closed ,Cookie will be erased.
  • Cookie data will be accessible to all the web pages with client request.
  • .Net providing HTTPCookie class for sending Cookie or Reading Cookie
Sending Cookie :

example: Httpcookie obj= new httipcookie("n")
                                           obj.value="100";
//Cookie can represent only String data.
      Response.AppendCookie(obj);
//It will send cookie to client Browser.

Reading Cookie submitted by Client (Browser) :
  
   Example:   HttpCookie obj=null;
                         obj=Request.Cookie["n"];
//It returns cookie class object 
//If cookie is available with request

If(obj==null)
  //no cookie submitted
else
obj.value
//returs Cookie value


Persistent Cookie :  Cookie placed with in hard disk memory of client system is called persistent cookie.
  • This will be maintained by client system with particular life time .[Life time will be 1 min,1 hour, 1 year .........]
  • Once Life time is finished cookie will be erased by client system operating system.
example:  Httpcookie obj = new Httpcookie("n");
                                           obj.value="100";
                     obj.expires=DateTime.Now.AddDay(2)  // lifetime is 2 days
                       Response.AppendCookie(obj);
//Persistent cookie= in-memory cookie + Life time




         

No comments:

Post a Comment