What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 46000 |  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: 26423 | 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

How to upload a image and store in a database

Express edition are downsized free IDE provided by Microsoft, so that developers and experiment to feel how the enterprise edition will look like. One of the products provided in the express edition suite is the web developer 2008 express edition. VS web developer edition helps us to make web application in .NET. One of the big drawbacks of VS web developer express edition is that it helps to debug but does not compile the ASPX pages. In other words no DLL is generated for the ASPX pages. This tutorial will discuss how we can use the aspnet_compiler.exe to generate DLL for web projects developed in VS 2008 web developer express edition.

To get the primary ke value of the GridView records in JavaScript, we can follow this approach.

This article explains how to ensure that submit button is clicked only once, get confirmation from user before submitting the form and display "Please wait" message when the ASP.NET web form is submitted to the server. The sample demo also works well when you have many asp.net validation control in your form.

Here i am trying to help you to how to open and read the EXCEL File and bind the data to gridview server control in asp.net using C# language. Its very easy way compared to others.

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/22/2013 6:44:28 AM