What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 220 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to set the expiration datetime for the cookies in ASP.NET?

How to set the expiration datetime for the cookies in ASP.NET?

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


In this article, we are going to learn how to set the expiration date and time for the cookies created in 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 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

<p><asp:Label ID="lblTime" runat="server" EnableViewState="false" /></p>

<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 code snippet, we a Label and two buttons. Label is used to write the current date time on the page. The first button executes SetCookie server side method and second button executes GetCookie server side method.

CODE BEHIND

 

protected void Page_Load(object sender, EventArgs e)

{

lblTime.Text = DateTime.Now.ToString();

}


protected void SetCookie(object sender, EventArgs e)

{

// set the cookie

Response.Cookies["MyCookie"].Value = "My cookie data";

Response.Cookies["MyCookie"].Expires = DateTime.Now.AddSeconds(10);

}


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.

SetCookie method

In the SetCookie server side method, we are setting the cookie value and setting the Expiry date of the cookie by setting the Expires property. This will set the expiry date to current date time + 10 seconds, it means that this cookie should expire (the cookie should be deleted from the browser) in 10 seconds.

GetCookie method

This method first checks for the “MyCookie”, if it is not null then get the cookie value using the Request object and writes on the page.

OUTPUT

Hope this article was useful. Keep reading ... In the next article we shall learn how to read and write multi-valued cookies in ASP.NET.

Thanks for reading.

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:08:29 AM | Points: 25

Hi sir,
I am using this code below:-
Response.Cookies["MyCookie"].Value = "My cookie data";

Response.Cookies["MyCookie"].Expires = DateTime.Today;
string Value = Request.Cookies["MyCookie"].Value;
Response.Write(Value);


I want that my cookie should be valid throughout the day. But after some seconds if i click the getcookie method then value is gone.....!
Can you please explain, what i am doing wrong ?

Thanks and Regards
Akiii

Posted by: SheoNarayan | Posted on: 21 Jun 2011 12:16:34 AM | Points: 25

Set the Expires property of the Cookies to more than today. So you can write something like

Response.Cookies["MyCookie"].Expires = DateTime.Now.AddDays(1);


and it should work.

Thanks

Posted by: Akiii | Posted on: 21 Jun 2011 10:48:02 AM | Points: 25

yes sir it working....

thanks for the help..
Akiii

Posted by: SheoNarayan | Posted on: 21 Jun 2011 11:16:58 AM | Points: 25

Great, happy to hear this.

Thanks

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

In this article we shall learn how to limit the maximum number of characters allowed in the Mult-iline textbox, how to avoid text wrapping into the Multi-line textbox, how to apply CSS style into the asp:TextBox and how to set the default focus on the TextBox when page loads.

In this tutorial, we will learn to create our own custom template which can be used for creating web sites

In this article, we shall see how to sort columns data in the GridView.

I am going to explain how to build dynamic menus using XMLDataSource Asp.Net ,vb.net.

Guys has anybody faced problems with the Key, value pair collections not sorting the collections based on values . As all the collections sort themself on keys . As i needed a collection of Key,Value pairs which does sorting on values rather than key, For that reason i have created my own collection class which dose sorting on the values. And this is working perfectly for me .

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 9:21:59 AM