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

Sheonarayan
Posted by in ASP.NET category on for Advance level | Points: 250 | Views : 38972 red flag
Rating: 5 out of 5  
 1 vote(s)

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.

Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 6/21/2011 | 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 on: 6/21/2011 | 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 on: 6/21/2011 | Points: 25
yes sir it working....

thanks for the help..
Akiii
Posted by: SheoNarayan on: 6/21/2011 | Points: 25
Great, happy to hear this.

Thanks

Login to post response

Comment using Facebook(Author doesn't get notification)