Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29784 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to read cookies in ASP.NET?

How to read cookies in ASP.NET?

2 vote(s)
Rating: 5 out of 5
Article posted by Sheonarayan on 6/16/2011 | Views: 10926 | Category: ASP.NET | Level: Advance | Points: 250 red flag


In this article, we are going to learn how to read cookies in different ways using ASP.NET.


Introduction

A Cookie is a small amount of text that attached to the requet and response in between browser and the server. This small amount of text can be read by the application whenever user browse the it. Please note that cookie is not a good medium to store the confidential data as it is stored into the user browser.


ASPX PAGE

<asp:Button ID="btnCreate" runat="server" Text="Read Cookie - One way" OnClick="ReadCookie1" />

<asp:Button ID="Button1" runat="server" Text="ReadCookie - Other way" OnClick="ReadCookie2" />

In the above code snippet, we have two buttons. The first button executes ReadCookie1 method and second button executes ReadCookie2 method.

CODE BEHIND

protected void ReadCookie1(object sender, EventArgs e)

{

HttpCookie cookie = Request.Cookies["CookieKey1"];

Response.Write(cookie.Value);

}


protected void ReadCookie2(object sender, EventArgs e)

{

Response.Write(Request.Cookies["CookieKey2"].Value);

}

 

Get hundreds of ASP.NET How to Tips and Tricks.

ReadCookie1 method

In this method, we are retrieving the cookie using Request object and saving into HttpCookie variable. The next line writes the cookie value on the page.

ReadCookie2 method

In this method, we are retrieving the cookie value using Request.Cookies collection by passing the Cookiename as parameter.

OUTPUT


In this article, we learnt how to read the cookies. To know how to set the expiration date time of the cookies, click here.

To learn how to create cookies in ASP.NET, click here.

Thanks. If you like this article, share it to your friends by tweeting or sharing it via facebook, linkedin or other websites.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

 Responses
Posted by: Naimishforu | Posted on: 22 Jun 2011 12:52:53 AM | Points: 25

Nice article! Thanks, it had helped much.

Posted by: Lakn2 | Posted on: 29 Jun 2011 05:22:40 AM | Points: 25

good one

>> Write Response - Respond to this post and get points
Related Posts

Many times we want certain set of validation to fire for some user and certain set of validation to fire for some other user. In the section we will see how we can achieve the same using Microsoft enterprise validation blocks.

This atricle explains how we can use C# and VB.NET class together in any application.

Here is a very simple example of how to split a string delimited with a particular character or string. I know this is very simple to do but I have find many people struggling in doing that. Attached code also demonstrate how to use StringBuilder, Foreach loop, Regex etc.

In this article we shall learn how to validate a valid email address entered into the textbox, how to compare two textboxes for equality, like password and confirm password, how to perform custom validation in ASP.NET and how to display all validation failed errors in the Alert box or as a summary.

In this article, I woul like to explain how we can retreive the data through ado.net entiry frameowk by using stored procedure.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/28/2012 11:55:12 AM