creating temporary cookie with expiry time asp.net 2.0

Hello

I think I've confused myself here. I have read that asp.net creates cookies as temporary cookies if you don't set the expiry time. How then, can you have a tempoary cookie that expires after an hour. I know this is possible but it would seem to me that by setting the expiry time for an hour you are making the cookie permanent not temporary?

also, how do browsers distringuish between temp and perm cookies? How do they know when to keep the cookie on closure or to delete it. There must be some sort of flag within the cookie

thanks
andiejeAsked:
Who is Participating?
 
andiejeAuthor Commented:
I have found the answer elsewhere. You cannot directly create a temporary cookie with an expiry time. If you want to create a temporary cookie with an expiry time you have to store some information about expiry time in the actual cookie value. This is how the forms authentication module/forms authentication ticket works.
Thanks for your help
0
 
wht1986Commented:
see this article http://tempestini.net/2007/08/18/permanent-vs-temporary-cookies/

has a nice little write up on it, basically its just if an expires property is set it become a temp cookie
0
 
wht1986Commented:
one other link http://www.wisegeek.com/what-are-computer-cookies.htm

basically if the expiration is set, the browser holds the cookie in memory rather than on the hard dick it appears
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
andiejeAuthor Commented:
Hi
Thanks for your replies but I'm afraid that the web links don't answer my question. I specifically want to know how you create a temporary cookie with an expiry time in asp.net 2.0. Perhaps it is not possible

andrea
0
 
wht1986Commented:
// c# code

// temporary cookie, dropped by browser on close
HttpCookie cookie = new HttpCookie("TestCookie");
cookie["setting1"] = "mysetting1value";
Response.Cookies.Add(cookie);

// permanant cookie with expiration at 10 years from now
HttpCookie cookie = new HttpCookie("TestCookie");
cookie["setting1"] = "mysetting1value";
cookie.Expires = DateTime.Now.AddYears(10);
Response.Cookies.Add(cookie);
0
 
andiejeAuthor Commented:
Hi
Thanks for your reply but I'm not sure you have understood the question properly. I want to create a temporary cookie that expires in an hour. In your answer you have created a temporary cookie that has no expiry and a permanent cookie that expires in ten years.
Many thanks
0
 
wht1986Commented:
Ah, i dont believe what you want can be done quite in the fashion. But why would you want that, the purpose of a temporary cookie is to be destroyed after the browser closes.  If you want the cookie on the browser session to expire after an hour i would do something like, create a temporary cookie and set the time the cookie was created into session at the begining of session start (or wheneer you need to set it). Then with each request to the page check to see if more than an hour as expired on the cookie.


protected void Session_Start(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("TestCookie");
cookie["setting1"] = "mysetting1value";
Response.Cookies.Add(cookie);
Session["LastCookieSet"] = DateTime.Now;
)
 
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Session["LastCookieSet"] != null)
            {
                DateTime lastSet = (DateTime)Session["LastCookieSet"];
                if (DateTime.Now.Subtract(new TimeSpan(1, 0, 0)) > lastSet)
                    HttpContext.Current.Response.Cookies.Remove("TestCookie");
// do something because the cookie is no longer valid
            }
        }

Open in new window

0
 
andiejeAuthor Commented:
My purpose for asking was to find out how the forms authentication ticket works because that can clearly timeout after a minute or an hour and it uses both temporary and permanent cookies. The FA ticket works as I described in my last post by setting information about the cookie expiry in the actual value part of the cookie
0
 
wht1986Commented:
form authentication ticket is not quite a cookie, but can be used by one, theres a link here that describes it a bit more. Maybe that will help with what you are trying to do

http://support.microsoft.com/default.aspx/kb/910443
0
 
wht1986Commented:
Basically its a derivative of what i was showing.  The forms authentication ticket can be placed inside a cookie. that cookie can be either temporary or persisted.  the authentication request would then pull the forms authentication ticket out of the cookie and examine if the auth ticket had expired.
0
 
wht1986Commented:
Did that help andieje?
0
 
andiejeAuthor Commented:
hi, i think i had already answered the question myself by saying that what I was asking was not possible. but thank you for your time in answering
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.