Introduction of the cookies in ASP.NET

No.of Views783
Bookmarked0 times
Downloads 
Votes0
By  NikhilJohari   On  16 Feb 2010 03:02:18
Tag : ASP.NET , HTML and CSS
Introduction of the cookies in ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Cookies provide a useful means in Web applications to store user-specific information. For example, when a user visits your site, you can use cookies to store user preferences or other information. When the user visits your Web site another time, the application can retrieve the information it stored earlier. 

There are three types of classes which allows to work with cookies.

1- HttpCookie
2- HttpRequest
3- HttpResponse

HttpCookie
This cookie property allows to create and Manupulate individual Http cookies in asp.net

HttpRequest
This property allows to access cookies from client machine

HttpResponse
This property allows to save and create cookie in client machine.


HttpResponse and HttpRequest

 

  • How To create cookies in asp.net


Response property is used to create cookies.So here is simple cookie using Response.

Response.Cookies("Name").Value = "Jason Bourne";

 

This line is saving the cookie where the cookie name is"Name" and the value is "Jason Bourne" in client machine. 

  • How to set the time of expiration of cookie
Response.Cookies("Name").Expires = DateTime.Now.AddDays(1);


This line is using to set the expire date of this cookie which is 1 day.

  • How to retrieve values from cookies
string name = this.context.Request.Cookies("Name").Value;



This line of code is retrieving the value from the cookie and stroing the string variable name and the value of cookie is "Jason Bourne" which we were saved before.

If this cookie dosen't have any value then the string will return empty. 

HttpCookie

HttpCookie is also a very useful class in creating and retrieving the values from cookies. 

HttpCookie cook = new HttpCookie("Name");
cook.Value = "Jason Bourne";

cook.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cook);


This line is using to set the expire date of this cookie which is 1 day.

Conclusion

We have learned how to create cookies, Retrieve the values from Cookies, How to set the expire time of cookies etc.

 
Sign Up to vote for this article
 
About Author
 
NikhilJohari
Occupation-
Company-
Member Type-Senior
Location-India
Joined date-15 Aug 2009
Home Page-http://fast-get.com
Blog Page-http://dotnetask.blog.co.in
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top