Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29880 |  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: 3429 | 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

Creating ASP.Net Menu Control using CSS

Code review is one of the important activities in any project. Manual code reviews are definitely good and I will say better than automation. But when the project is large manual code reviews have their own limitations. FXCOP is one of the legendary tools which help us automate code reviews. This article will discuss some basics of FXCOP and then concentrate mainly on how we can add custom rules in FXCOP.

In this article, we are going to learn how to store view state at server side in a file and retrieve the state for asp.net page processing.

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

SQL Server is a very large subject in itself. Rather than attempting to cover all aspects of SQL Server database access, this article focuses on those areas where you are likely to gain the biggest payoffs.

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:15 AM