What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 24502 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to limit the scope of cookies to a specific folder of the website in ASP.NET

How to limit the scope of cookies to a specific folder of the website in ASP.NET

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


In earlier articles, we learnt how to create, read, expire cookies. In this article we are going to learn how to limit the scope of the cookies to a particular folder of the website or particular sub-domain of the website.

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.

Let's learn how to limit the scope of the cookie creation so that those cookie will be available only to a particular folder of the website. 

I am going to create a sample demo page here and below is the code for that.

ASPX PAGE

<asp:Button ID="btnCookieSet" runat="server" Text="Set Cookie" OnClick="SetCookie" />

<asp:Button ID="btnCookieGet" runat="server" Text="Get Cookie" OnClick="GetCookie" />

In the above snippet, we have two buttons. Clicking on first button executes SetCookie method and clicking on the second button executes GetCookie method.

CODE BEHIND

// how to test

// Access this page and set the cookie, then get the cookie value.

// Now go to any page that is not into this folder and try to read the cookie created here, you will not get it.

protected void SetCookie(object sender, EventArgs e)

{

// set the cookie

Response.Cookies["MyCookie"].Value = "My cookie value under MyCookieFolder folder";

Response.Cookies["MyCookie"].Path = "/Cookies/MyCookieFolder/";

}


protected void GetCookie(object sender, EventArgs e)

{

if (Request.Cookies["MyCookie"] != null)

{

string cookieValue = Request.Cookies["MyCookie"].Value;

Response.Write(cookieValue);

}

}

Get video tutorials of hundreds of ASP.NET Tips and Tricks like this.

SetCookie method

In the SetCookie method we are setting the cookie value and then setting the cookie path. This path should be from the root folder of the website/application. Setting path of the cookie restricts this cookie to be created only for that folder. This cookie can’t be accessed by any page that is outside this folder (in this case my folder is /Cookies/MyCookieFolder/).

GetCookie method

It simply checks for the Cookie and if it is not null, retrieves its value and writes on the page. As this method is inside the same page so you should get the cookie value set from the SetCookie method. 

Now, you copy-paste this method into other page that is not into the /Cookies/MyCookieFolder/ folder and try to execute this method, you will NOT get the cookie value. This is because the cookie was created for /Cookies/MyCookieFolder/ folder only and will not be available outside this folder.

OUTPUT

In which scenario, we should limit the scope of the cookies?

When cookies are created for the website, all the cookies are transferred to and from for all the requests to the server from client. In case your application is so big and relies on cookies its not good practice to send all cookies to and from the server (it may hamper the performance of your web pages as well). So in this case you can limit the scope of cookies based on the path or folder so that only folder specific cookies will be sent to and from the server.

There might be another scenario as well. Lets take an example where your application has several modules residing in several folders of the web application/website. In this case also you can create folder specific cookies so that cookie created for one module doesn't get affected by others.

In case you have missed other articles like creating, reading, expiring cookies, click here.

Hope this article was useful. Subscribe for the RSS to get more articles in this series. In the next article we shall learn some more interesting stuffs related with cookies. 

Thanks for reading, keep learning and sharing ....

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: Akiii | Posted on: 21 Jun 2011 12:26:39 PM | Points: 25

Hi sir,
Thanks for your article.
Can you tell me what is the real-life need in restricting a cookie to a particular folder or a sub-domain ?

Hoping to get more articles like this....
Regards
Akiii



Posted by: Akiii | Posted on: 21 Jun 2011 12:31:57 PM | Points: 25

Sir in the paragraph...

"GetCookie Method

Now, you copy-paste this method into other page that is not into the /Cookies/MyCookieFolder/ folder and try to execute this method, you will get the cookie value . This is because the cookie was created for /Cookies/MyCookieFolder/ folder only and will not be available outside this folder."

I think the bold part should be "you will not get the cookie value".....

I hope i am correct....

Regards
Akiii

Posted by: SheoNarayan | Posted on: 21 Jun 2011 09:26:24 PM | Points: 25

Hi Akiii,

Thanks for your response and pointing out the typo, I have updated.

I have also added "In which scenario, we should limit the scope of the cookies?" section in the article, please go through it to know the real-time scenario for restricting the scope of the cookies.

Keep reading and sharing!

Regards

Posted by: Akiii | Posted on: 22 Jun 2011 12:05:02 AM | Points: 25

Thank you very much sir for the updation...

Regards
Akiii

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

Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.

This article explains how to create a .pdf document in .NET using iTextSharp. After reading this article, you should be able to dynamically export GridView into .pdf document. You should also be able to generate .pdf with some custom text and images.

In this section we will touch base on one of important concepts in ASP. NET.

In this article I have shown power of ASP.NET to export data to excel, pdf and doc. Other softwares that uses these extension gives flexibility of sorting, searching and security. In a article you will get some good tricks to Export data in excel, documents & PDf Files.

To get the CheckBoxlist Value using Javascript, we have to update the checkboxlist into a new control say name:"CheckBoxListExCtrl " which can inherit checkboxlist property.

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 3:25:28 PM