C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Cookie in ASP.NET


Posted Date: 17 Mar 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: KasiviswanathMember Level: Gold    
Rating: 1 out of 5Points: 7



The cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. One type of cookie is called as Browser cookies.
The Browser cookie is used to store small pieces of information on a client machine. A cookie can store only up to 4 KB of information. Normally cookies are used to store frequently used data. (eg) User id, password

There are two types of browser cookies – session and persistent.

1. Session cookies, also called as temporary cookies are stored in browser’s memory and stay alive as long as a browser session is live. When you close a browser, these cookies die.

2. Persistent cookies stored on the hard drive with a date information and can live for a long time. We can specify a date as the life period of the cookie, when we are creating that cookie. Even though it’s not certain that a cookie will live as long as you specify.

Creating Cookies


1.
Response.Cookies("userName").Value = "Spider_User"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(2)

2.
Create user id and password cookies
Dim cookie_test As HttpCookie = New HttpCookie("Userid")
cookie_test.Value = "Spider_user"
cookie_test.Expires = #30/03/2004#
Response.Cookies.Add(cookie)
cookie_test = New HttpCookie("Pwd")
cookie_test.Value = "test"
cookie_test.Expires = #30/03/2004#
Response.Cookies.Add(cookie)



Read cookies values




Dim cookieCtrls As New HttpCookieCollection()
cookieCtrls = Request.Cookies

Dim str As String
For Each str In cookieCtrls
ListBox1.Items.Add("Cookie: " + str)
ListBox1.Items.Add("Value:" & _
Request.Cookies(str).Value)
Next




Remove Cookies



Request.Cookies.Remove("Pwd")
Request.Cookies.Remove("Userid")

(or)

We can set date -1 value as the cookie expires date for removing the cookie

Dim i As Integer
Dim cookieName As String
Dim limit As Integer = Request.Cookies.Count - 1
For i = 0 To limit
aCookie = Request.Cookies(i)
aCookie.Expires = DateTime.Now.AddDays(-1)
Response.Cookies.Add(aCookie)
Next




Multi-Valued Cookies using Subkeys



Response.Cookies("user")("Name") = "mike"
Response.Cookies("user")("ModifiedDt") = DateTime.Now.ToString
Response.Cookies("user").Expires = DateTime.Now.AddDays(2)



Store Cookies in the Server Folder.


Suply place to store the cookie using path property




Dim appCookie As New HttpCookie("testAppCookie")
testAppCookie.Value = "written " & Now.ToString
testAppCookie.Expires = Now.AddDays(1)
testAppCookie.Path = "/Applicationpath"
Response.Cookies.Add(testAppCookie)







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Registering HTTP Handler
Previous Resource: User Controls
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use