Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

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
Avatar of wht1986
wht1986
Flag of United States of America image

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
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
Avatar of andieje
andieje

ASKER

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
// 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);
Avatar of andieje

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of andieje
andieje

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of andieje

ASKER

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
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
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.
Did that help andieje?
Avatar of andieje

ASKER

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