We recently realized that our session cookie is being written out to the fully qualified domain name of our site, www.myapp.com
, for example:
MYAPPCOOKIE: 79D5DB83..., domain: www.myapp.com
We want to switch this to being a cookie that can be shared cross subdomain, so any server at myapp.com
can use the session cookie as well. For example, we'd like our cookie to store like so:
MYAPPCOOKIE: 79D5DB83..., domain: .myapp.com
We tried just changing our session cookie to use that domain like so:
Cookie sessionCookie = sessionManager.getSessionIdCookie();
sessionCookie.setDomain(".myapp.com");
and this works fine in most cases.
We're finding some users can't login after deploying this new code in some situations. The problem arises when a user:
- has been logged into our site in their current browser session, but aren't currently logged in.
- they try and login again
It appears there are 2 session cookies in their browser:
a stale cookie from their previous session, with the fully qualified domain name
MYAPPCOOKIE: 79D5DB83..., domain: www.myapp.com
the new session cookie for the session they just logged into, with the new domain setting
MYAPPCOOKIE: 79D5DB83..., domain: .myapp.com
What's the best way to manage this old cookie being around? We've tried adding some code to delete the old cookie if a user doesn't have a session, but there are some paths into our app where this doesn't seem to work.
We're open to renaming the cookie if that might work, and are looking for any suggestions others may have. Thanks!