Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 14346 |  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: 26303 | 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

Posted by: Niranjan44 | Posted on: 09 Jan 2013 10:16:09 PM | Points: 25

You can also find here in simple way

http://www.programcall.com/6/dotnet/cookie-in-.net.aspx


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

LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again.

This article is a quick FAQ of Agile. By reading this you will understand fundamentals of Agile and different ways of implementing Agile.

[B]Introduction[/B] Performance tuning can be tricky. It's especially tough in Internet-related projects with lots of components running around, like HTML client, HTTP network, Web server, middle-tier components, database components, resource-management components, TCP/IP networks, and database servers. Performance tuning depends on a lot of parameters and sometimes, by changing a single parameter, performance can increase drastically.

When new technologies are introduced one of the biggest business challenges faces is migrating the current projects to new technologies with least effort. I am sure scratch development is not always a preferred option from business investment point of view. One of the new technologies which is talked most is ‘LINQ’. This article will focus on how we can convert simple existing .NET classes in to LINQ enabled classes.

This article describe how to use Forms Authentication in ASP.NET with C#. After reading this article you will be able to create a web application with Forms Authentication. This article also includes downloadable sample project with source code.

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 find 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/18/2013 8:41:49 AM