Persistent and Non Persistent cookies

Posted by Rajasekhar0544 under ASP.NET on 12/11/2012 | Points: 10 | Views : 8320 | Status : [Member] | Replies : 3
Hi All,

Could you please tell me the persistent and non persistent cookies with an example.
Provide the default expiration time of Non persistent cookie.
I have found many forums explaining this but im unable to figure it out. so kindly help me.



Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 25

Persistent cookies - These remain on your hard drive until you erase them or they expire.
Non-Persistent cookies - Are stored in RAM on the client and are destroyed when the browser is closed.

Pavan Kumar
Mark Answer if this fits the need

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/11/2012 [Member] [MVP] Gold | Points: 25

Persistent cookies:- which are stored in the client hard-drive until they expire. Persistent cookies should be set with an expiration dates. Sometimes cookies stays until the user deletes the cookies.

//Creting a Cookie Object
HttpCookie _EmpInfoCookies = new HttpCookie("EmpInfo");
//Setting values inside it
_EmpInfoCookies["UserName"] = "Ajit";
_EmpInfoCookies["UserColor"] = "Green";
//Adding Expire Time of cookies
_EmpInfoCookies.Expires = DateTime.Now.AddDays(1);
//Adding cookies to current web response

Non-persistent Cookies:- These can be called temporary Cookies. If there is no expiration time defined, then the cookie is stored in the browser memory.

//Creting a Cookie Object
HttpCookie _EmpInfoCookies = new HttpCookie("EmpInfo");
//Setting values inside it
_EmpInfoCookies["UserName"] = "Ajit";
_EmpInfoCookies["UserColor"] = "Green";
//Adding cookies to current web response

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/23/2013 [Member] [MVP] Silver | Points: 25

Cookies: - Cookies are little pieces of information that a server stores on a browser. They are of two types
1. Temporary cookie
2. Persistent cookie

Temporary cookie: - They are also known as session cookies. These are volatile in nature. When the browser is shutdown they are erased.

Persistent cookie:- These may be called as permanent cookies. These are especially saved in files. It may remain for a month or year.

Properties of cookies
Some properties of cookie
Name: - represent the name of cookie.
Name value: - represent a collection of key values of cookie
Domain: - represent the domain associated with a specific cookie.
Path: - the path associated with a cookie.
Expires: - expired time of cookie.
Hashkey: - identifies whether the cookie is a cookie dictionary.
Secure: - specifies whether the cookie is to be sent in an encrypted connection or not.

Working with cookies
Cookies are send back and forth between a browser and server. The server creates a cookie by using the set cookie header. Subsequent request from the browser is send to the server in cookie header. Suppose the server create a cookie

Set cookie: username=rajkumar; path=1;; expires=Friday, 19th august 20116:30:00GMT
The server creates the cookie named as username containing the user information. Path identifies any requested page to the website, domain identifies the specific website, expired identifies that this cookie would be expired on the specific data mentioned.
After the server creates the cookie the response is send to the server from the browser for each subsequent request to the webpage. The reader sends in the format cookie: username=raj+kumar
The server is using set cookie reader. The browser is using reader.

Creation and reading session cookies
The session cookies can be created by using the response object and can be read by using the request object. The cookies properties of the response and request objects contains instances of Httpcookies collection class. Each cookie is created by using Httpcookie class.

For creating cookies
Dim cookies1 As New HttpCookie("raj", "kumar")
For reading cookies


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'For creating cookies

Dim cookies1 As New HttpCookie(TextBox1.Text, TextBox2.Text)
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'For reading cookies
Dim s As String
For Each s In Request.Cookies
Label1.Text += "<li>" & s
End Sub

Sub clear()
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class

Persistence cookies
It is saved in a config has a specific expiration date. It is especially used to store information about userid, name of authenticated user.

For creating persistence cookie
Dim pcookie1 as New HttpCookie("user name"."ravi kumar")
pcookie1.expires= #19/06/2011#

For reading persistent cookie
Response.Write(Request.cookies("user name").Value)

Limitation of cookies: -
1.A cookie can store maximum of 4kb of data.
2.A cookie can contain only string values. It can contain objects like dataset object.
3.A cookie is browser dependent.
4.Persistent cookie may be deleted before expiration date by browser.

1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
1. Users can delete a cookies.
2. Users browser can refuse cookies,so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies

If this post helps you mark it as answer

Rajasekhar0544, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response