I am trying to set cookies to never expire (even after browser has been closed) in Google App Engine using the documentation for webapp2 sessions and building a response. I've tried:
Code snippet 1:
from webapp2
from webapp2_extras import sessions
class MainHandler(webapp2.RequestHandler):
def never_expire_session(self):
sessions.get_session(name='my_session', max_age=2147483647)
@login_required
def get(self):
....
html_template = JINJA_ENVIRONMENT.get_template('index.html').render(myapp)
self.response.out.write(html_template)
OR
Code snippet 2:
class MainHandler(webapp2.RequestHandler):
def never_expire_session(self):
return self.response.set_cookie('my_session', max_age=2147483647)
@login_required
def get(self):
....
html_template = JINJA_ENVIRONMENT.get_template('index.html').render(myapp)
self.never_expire_session()
self.response.out.write(html_template)
Code Snippet 2 results in a response of NoneNone
. (Not sure the output of the first code snippet.)
I have also tried the answer here, that recommends configuring the parameters:
config = {}
config['webapp2_extras.sessions'] = {'session_max_age': 2147483647}
....
....
app = webapp2.WSGIApplication([
....
], debug=True, config=config)
I've cleared my cookies, restarted my browser, and checked the console and the cookie value shows as a randomly generated sequence of numbers and letters and the expiration of the cookie is set to one year from datetime.now
. Nothing I've tried works.
How can I correctly set the session cookie so it never expires, even on browser exit?
I was experiencing a caching issue which prevented my session cookie from showing in the developer console and being applied. After erasing the browser history and restarting my computer, it cleared the cache. Eventually I got it to work with the following:
from webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
the_value = ''.join(SystemRandom().choice(string.digits _ string.ascii_lowercase) for_in range (50))
max_age = 2147483647
# max age for Internet Explorer
cookie_expires = datetime.datetime.now() + datetime.timedelta (200)
return self.response.set_cookie('my_cookie', the_value, max_age=max_age, httponly=True, secure=True)
NOTE: if you want to apply this change to a specific cookie, rename "my_cookie" to that cookie (i.e. "SACSID" a cookie on many Google applications)