.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Cookies in aspnet

Posted By:satyapriyanayak       Posted Date: April 01, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Cookies in aspnet

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;domain=abc.com; 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 abc.com, 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 file.it 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


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend