IT Community - Software Programming, Web Development and Technical Support

How to set time for cookie in ASP.NET?

This is a discussion on How to set time for cookie in ASP.NET? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi all, How to set time for cookie? Is any sample?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-21-2007, 06:11 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Question How to set time for cookie in ASP.NET?

Hi all,

How to set time for cookie?

Is any sample?
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-21-2007, 07:43 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Red face Re: How to set time for cookie in ASP.NET?

Yes,

You can easily set the time when a cookie expires. We'll set the Expires property of myCookie to the current time number of hours:

Code:
myCookie.Expires = DateTime.Now.AddHours(2);
This cookie will expire in two hours starting now.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-21-2007, 08:46 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Exclamation Re: How to set time for cookie in ASP.NET?

Can we add a week in our cookie?

Is it possible?
__________________
thanx n regards
jeyaprakash.c

Last edited by jeyaprakash.c : 01-09-2008 at 01:12 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-21-2007, 08:52 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: How to set time for cookie in ASP.NET?

Yes u can,

for that use as following.,

Code:
myCookie.Expires = DateTime.Now.AddDays(7);
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-21-2007, 10:11 PM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: How to set time for cookie in ASP.NET?

<script runat="server">
void WriteClicked(Object Sender, EventArgs e)
{
//Create a new cookie, passing the name into the constructor
HttpCookie cookie = new HttpCookie(NameField.Text);

//Set the cookies value
cookie.Value = ValueField.Text;

//Set the cookie to expire in 1 minute
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0);
cookie.Expires = dtNow + tsMinute;

//Add the cookie
Response.Cookies.Add(cookie);

Response.Write("Cookie written. <br><hr>");
}
</script>
<html>
<body>
<h3>Use the button below to write cookies to your browser </h3>
The cookies will expire in one minute.
<form runat="server">
Cookie Name <asp:textbox id="NameField" runat="server"/><br>
Cookie Value <asp:textbox id="ValueField" runat="server"/><br>
<asp:button text="WriteCookie" onclick="WriteClicked" runat="server" /><br>
</form>
<a href="readcookies.aspx">Read the cookies</a>
</body>
</html>
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-22-2007, 01:24 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: How to set time for cookie in ASP.NET?

Hi buddy....

A small information about cookies....

Sometimes you'll want to set a path for a cookie so that it will be available only for that path in your website. You can set a cookie's path with the Path property:

Code:
myCookie.Path = "/test";
I think this will help u for working with cookie....
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-22-2007, 01:27 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: How to set time for cookie in ASP.NET?

Another one information in cookie is here.....

We can set the domain for a cookie as follows,


Code:
myCookie.Domain = "test.com";
You can work with subdomain also...as follows,

Code:
myCookie.Domain = "forums.test.com";
Hope this will also making u as a genius in cookie....
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-22-2007, 04:57 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Question Re: How to set time for cookie in ASP.NET?

Hi vinoth,

Yes you are making me as a genius in cookie....

And I hv another one doubt....

that is...How to edit a cookie?
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-08-2008, 09:49 PM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: How to set time for cookie in ASP.NET?

Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
// the above creates the cookie, or overwrites it if the cookie already exists.

HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-16-2008, 10:52 PM
rrrajesh84in rrrajesh84in is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 399
rrrajesh84in is on a distinguished road
Default Re: How to set time for cookie in ASP.NET?

hi all


it really helps me but one doubt, what happens when we create a cookie in the name which has already exists.

thanks in advance
__________________
.....................................
''''''
Rajesh''''''
Ants. . . . . . Like me
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
show clocks, one showing the current time, and another showing a time 12 hours behind itbarota HTML, CSS and Javascript Coding Techniques 1 02-13-2008 10:15 PM
How can I set the time zone for MySQL to UK time? itbarota PHP Programming 1 09-12-2007 01:13 AM
How can I set a cookie in JSP? anbuchezhians Java Server Pages (JSP) 1 07-27-2007 03:02 AM
Google to shorten time 'cookie' data is stored senthilkannan Google 0 07-19-2007 05:01 AM
Diff between Transaction-time and Server response-time vadivelanvaidyanathan Software Testing 0 03-21-2007 07:29 AM


All times are GMT -7. The time now is 02:18 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0