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


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Cookies In ASP.NET

Posted By:Nikhil Kumar       Posted Date: January 12, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article tells us about cookies in asp.net. There are total three types of classes which allows to work with cookies. and in this article we will see the details of these 3 types of cookies.
 

Cookies In ASP.NET;-

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,

 
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 save 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);

Conclusion

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

Thanks !!
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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