- A Cookie is similar to a variable used to transfer small amount of data to maintain the state (of the Data).
- A Cookie is created by Server Side program ,stored in client machine memory even Cookies also can be created by using JavaScript but very rarely used .
- A Cookie once created will always travel from client request to server and server response to client.
- Cookies are in general stored the data with string type .Cookies are always Domain based/Web based i.e., Every cookie will know for which website it has been created.
- Whenever we send request to any website from a browser, browser will search for the available Cookies for that site within the current machine, collects all the cookies and then will send to Server.
- In general every cookie is capable of transferring 4096 bytes/4kb information.
- Every browser will allow maximum of 20 cookies for a single website. Then oldest cookie will be destroyed automatically.
- Every browser will allow maximum 400 cookies from all the websites together when the next Cookie is coming first cookie will be automatically destroyed.
- Cookies are two types
1) In Memory Cookies2) Persistent Cookies
[1] In Memory Cookies:-
- These cookies are stored in browser memory these cookies do not have any Expiry Date and Time.
[2] Persistent Cookie:-
- As long as user is working with the website these cookies will be available in the browser memory whenever user closes the browser of the website, in memory cookies will automatically deleted from browser memory.
- These Cookies will have Expiry Date and Time.
- These Cookies are stored in client Machine Hard Disk.
- Default timeout period of a persistent Cookie is 50 Years.
- Based on Requirement Expiry Data and Time for Cookies can be set.
- To create our own Cookies in ASP.Net we use a class known as HTTPCookies Class
Properties with HttpCookies Class:-
[1] Domain:-Used to set or get the required Domain Name.[2] Expires:-Used to make an In Memory Cookie as Persistent .Using this property we can set or get the expiry date and time for the cookie.[3] Name:-Used to set or get the required Name of the Cookie.[4] Path:-Used to set or get the Complete Name of the Cookie.[5] Secure:-his is a Boolean property default is false .when set to true Cookie will travel using SSL(Secure Socket Layer).[6] Value:-Used to set or get the value into the Cookie.
Steps to work with Cookies:-
If we want to create three cookiesA=20B=20.5UName=SampathFrom one page to other page like…….
In source page use the following steps:-1. Create object for HttpCookies Class
Sy:-ClassName objectName =new ClassName(string CookieName)
Ex:- HttpCookies obj1=new HttpCookies(“a”);
HttpCookies obj2=new HttpCookies(“b”);
HttpCookies obj3=new HttpCookies(“UName”);
2. Store the Required Data into Cookie
Sy:- ObjectName.value=”Data”;
Obj1.value=”9”;
Obj1.value=”99.9”;
Obj3.value=”sampath”;
3. Set other Property values(if Required)
4. Add this Cookies Collection of outgoing response object.
Sy:- Response.Cookes.Add(Cookie objectName);
Ex: Response.Cookies.Add(obj1);
Response.Cookies.Add(obj2);
Response.Cookies.Add(obj3);
5. Redirect the Request to Target Page
Sy:- Response.Redirect(“Target Page Name”);
Ex:- Response.Redirect(“Target.aspx”);
When we use “Response.Cookies.Add” Internally A Collection table will be created with the name Cookies like:-
Name Value Expires Domain Path Secure a 20 Null www.microsoft.com/Default2.aspx False b 20.5 Null www.microsoft.com/Default2.aspx False Name sampath Null www.microsoft.com/Default2.aspx False
When we Redirect the request to other page then this collection table will be transferred to the Target Page as usual
Accessing the Cookies in Target Page:-We know that cookies collection table will be transvered to the TargetPage .so we can access these cookies from collection table using index value/cookie name like.
Request. Cookies[0].ValueorRequest. Cookies[“a”].ValueRequest. Cookies[1].ValueorRequest. Cookies[“b”].ValueRequest. Cookies[2].ValueorRequest. Cookies[“UName”].ValueAdvantages Using Cookies:-[1] Simple implementation[2] No server resources are required[3] Wide range support[4] Configurable expiration rulesDis-Advantages Using Cookies:-[1] Cannot used to tansfer sensitive data[2] Size limitation[3] User configured refusals[4] Depending on client machine.
No comments:
Post a Comment