Persisting user cookie RRS feed

  • Question

  • User-296654088 posted

    I want to create user preferences cookies and persist them even after user session expires. In the cookie, I will store things like some of user preferences, their avatar URLs, etc. I want these cookies to stay on user's computer till they're removed -- either by users or my app.

    I use Facebook/Google authentication in my ASP.NET MVC app so I already use cookies but they expire when user ends his/her session. This is the behavior I want for cookies that are used for authentication purposes.

    The user preferences cookie should however persist until my app or user removes them.

    What is the right approach to doing what I want to do? I can think of two approaches:

    1. Do not expire the cookie but change a value in it to indicate that the user's session has expired.
    2. Create another cookie with a different name that stores user info/preferences.

    I wanted to see how others handled this.

    Tuesday, March 15, 2016 2:38 PM


  • User614698185 posted

    Hi Sam,

    You should either store the user preference in a Cookie or Session. If both cases, check if the value is not null. If it is not null, use the Cookie or Session value. If it is null, populate your Session / Cookie value with the result of your database call.

    This way you will only make this database call once.

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 16, 2016 5:14 AM