locked
How to set the cookie expiration time for page cookies? RRS feed

  • Question

  • Hi,

    I want to set the cookies expiration time at runtime from codebehind file So how to do it ?


    Thanks, Pranil Yambal | MCTS | Pranil.Yambal@hotmail.com
    Thursday, May 26, 2011 11:40 AM

Answers

  • One can iterate through cookies collection in following way

    HttpCookie aCookie;


    for(int i=0; i<Request.Cookies.Count; i++)
    {
        aCookie = Request.Cookies[i];

    aCookie.Expires = DateTime.Now.AddMinutes(2);

    }

    Also if a cookie has subkeys, one can use NameValueCollection to get subkey and corresponding values and set the expiry accordingly.

    Hope this helps.


    Thanks
    • Proposed as answer by Vishvvas Wednesday, June 1, 2011 6:39 AM
    • Marked as answer by Pranil Yambal Wednesday, June 1, 2011 9:30 AM
    Wednesday, June 1, 2011 5:59 AM

All replies

  • You'll get the best feedback to your web development in the ASP.NET forums:

    http://forums.asp.net/

    These links may be helpful:

    http://msdn.microsoft.com/en-us/library/system.net.cookie.expires.aspx

    http://msdn.microsoft.com/en-us/library/ms178194.aspx

     

    Thursday, May 26, 2011 12:54 PM
  • You have Expires  property for cookie u can set it at runtime for example if u want to set the expire time for two minutes then u can do it like

    yourcookie.Expires = DateTime.Now.AddMinutes(2);

    please mark it as answer if it helps u


    amit.murli
    • Proposed as answer by Amit Murli Wednesday, June 1, 2011 2:07 PM
    Thursday, May 26, 2011 1:09 PM
  • Hi, I have used Aspnet membership controls so not have the cookie name to set the time .So i want to set the expiration time for all cookies on application level.
    Thanks, Pranil Yambal | MCTS | Pranil.Yambal@hotmail.com
    Tuesday, May 31, 2011 11:17 AM
  • I am not aware of a way to set the expiration time for all cookies in an application, but there might be a switch for that somewhere.

    I do not understand why you can't get the auth cookie.  The FormsAuthentication class has a method "GetAuthCookie" which does just that.  There is also a SlidingExpiration switch in the forms section of the web.config file that can be used to set the expiration for all forms authentication cookies.

    • Proposed as answer by Amit Murli Wednesday, June 1, 2011 2:07 PM
    • Unproposed as answer by Amit Murli Wednesday, June 1, 2011 2:07 PM
    Tuesday, May 31, 2011 1:51 PM
  • One can iterate through cookies collection in following way

    HttpCookie aCookie;


    for(int i=0; i<Request.Cookies.Count; i++)
    {
        aCookie = Request.Cookies[i];

    aCookie.Expires = DateTime.Now.AddMinutes(2);

    }

    Also if a cookie has subkeys, one can use NameValueCollection to get subkey and corresponding values and set the expiry accordingly.

    Hope this helps.


    Thanks
    • Proposed as answer by Vishvvas Wednesday, June 1, 2011 6:39 AM
    • Marked as answer by Pranil Yambal Wednesday, June 1, 2011 9:30 AM
    Wednesday, June 1, 2011 5:59 AM
  • Hi,

    Thanks for reply.

    I have tried your code and its working.

    Can you tell me in which cookie the user credentails saved when we are using aspnet memebership in asp.net application.


    Thanks, Pranil Yambal | MCTS | Pranil.Yambal@hotmail.com
    Wednesday, June 1, 2011 9:29 AM