Monday, December 28, 2009

Using Cookies in Asp.Net



This article tells about cookies and how to write and read cookies in ASP.NET web application.

Cookies are small amounts of text that can be stored on the client pc by the browser.  The cookie can be accessed both at the client through the use of Javascript and also at the server.  While cookies can be a convenient way to store small bits of state information, it's important to keep in mind that the client browser may have been configured not to accept cookies.

In each cookie you can not save more than 4k of information. Remember clients can open cookies and see the content of a cookie so never save a password there. Cookies are usually used to save user preference information.



Cookies are used to store user specific information in form of text strings. When a user visits particular website some user specific information gets stored and this information can be retrieved later when the same website gets visited from same machine and can be sent to server. Cookies are associated with websites instead of individual pages.

Web developers can use cookies to store user preference and renders the customized page on next visit according to user's interest. There are browser imposed limitations regarding size of cookies and number of cookies per website which can be stored.


In ASP.NET cookies can be created using class HttpCookie in System.Web namespace.

Cookies are added to the Cookies collection of response object, the object which is responsible for keeping information to be rendered to browser. Example for writing and reading cookie

 //Create an instance of HttpCookie
Dim mycookie as new HttpCookie("userid")


 //Set parameters like value and expires
mycookie.Value = "1"
mycookie.Expires = DateTime.Now.AddMonths(1)

 //Add to the Cookies collection of response object
Response.Cookies.Add(mycookie)

 We can do same thing by directly setting values for Response.Cookies collection

Response.Cookies ("userid").value = "1"
Response.Cookies ("userid").Expires = DateTime.Now.AddMonths(1)

 The above code creates cookie and store it in user's drive for one month. The following code is used to retrieve a cookie later.

 Request object is used to send cookies information along with the request for page to server.

If Not Requet.Cookies("userid") Is Nothing Then
Dim a As String
a= Not Requet.Cookies("userid")
End If

Here first we are checking whether cookie exists or not because it can be deleted by user. Cookies won't work if user has disabled cookies in browser settings.


    



To check if the cookie is enabled or disabled on the client side we can use the following script on the first page of the application:

if ( Get_Cookie( 'test' ) )
{
document.write( 'cookies are currently enabled.' );
/*
this is an example of a set cookie variable, if
you want to use this on the page or on another script
instead of writing to the page you would just check that value
for true or false and then do what you need to do.
*/
cookie_set = true;
// and these are the parameters for Delete_Cookie:
// name, path, domain
// make sure you use the same parameters in Set and Delete Cookie.
Delete_Cookie('test', '/', '');
}
// if the Get_Cookie test fails, cookies
//are not enabled for this session.
else
{
document.write( 'cookies are not currently enabled.' );
cookie_set = false;
}


This would check if the cookie is enabled or not on the client browser.

2 comments:

  1. Quite a nice blog..... giving brief and educating information...but i have a request can u please give some small example code and compare it with sessions ? Thanks in advance !!!

    ReplyDelete
  2. the problem of data corruption in the files of specified format can be fixed by the how can i read destroyed mdf files utility

    ReplyDelete

Comments to this post

LinkWithin

Related Posts with Thumbnails