Asp.net Reading and writing value in cookies

Hi,

In my last post I wrote about and overview of cookie, there basic use and some of their limitation. In this post I will talk on how do we read and write cookies with the help of Asp.Net.

When a browser makes a request to the server it also send existing (at the browser end) cookies information for that website. In Asp.Net the HttpRequest (also referred as Request property in the page class) objects contains a collection of all the cookies sent by the browser.

We can read the cookies from the request object like this.

if (Request.Cookies["lastVisit"] != null)

string strlastVisit = Server.HtmlEncode(Request.Cookies["lastVisit"].Value);   

W should always check if the cookie exists or not before working with cookie. Because as mentioned in my last article the browser may not contain those cookies for many reason.

To write a new cookie to the browser we use the HttpResponse (also referred as Response property in the page class) Object. To add a new cookie we add a new cookie in the cookie collection of the response object. We can either create a new object of HttpCookie or directly add the cookie to the response object. Here is the code of the same

Response.Cookies["lastVisit"].Value = DateTime.Now.ToString();

Response.Cookies["lastVisit"].Expires = DateTime.Now.AddDays(30);

OR

HttpCookie aCookie = new HttpCookie("lastVisit");

aCookie.Value = DateTime.Now.ToString();

aCookie.Expires= DateTime.Now.AddDays(30);

Response.Cookies.Add(aCookie);

Both of the above code does the same work. They add a cookie to the response collection, which in turn will add the cookie in the browser if the browser supports them. In the above code we also add an expiry date for the cookie. The cookie will be used for next 30 days only after which it will expire and not be used. Note that the expiry date for a cookie is set only. The browser never sends the information of when will the cookie be expiring

This does the work for basic working with cookie in Asp.Net. In the next post I will talk about some of the more feature that are available with cookie in Asp.Net

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 1/20/2008 9:30:44 PM

Let us say that we are storing a boolean value in the cookie (IsAdmin=true) ... when the admin successfully login.
if the site security depends on this boolean value ... !

is it possible to copy & paste this cookie from one machine to another and do you think it will work with some modifications?

or even if we create new empty text file with this value ?!

is there any metadata or unique id associated or saved with the cookie text file ? (send from the server .. as a security and differentiate different machines/users)??

& Thank you.

Posted on 1/21/2008 6:16:12 AM

Hi,

I think browser keep the track of unifying the cookies. I think its browser responsibility to maintain the cookie from the site only and not arbitrary chanage in client side..

Posted on 2/29/2008 1:18:21 AM

Hi,
Its nice Vikram Thanks. But how to check whether cookies are blocked or not and how to write an alternate code if cookies are blocked.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia