Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share a session across multiple subdomains in ASP.NET?

I have an application where, in the course of using the application, a user might click from

virginia.usa.com

to

newyork.usa.com

Since I'd rather not create a new session each time a user crosses from one subdomain to another, what's a good way to share session info across multiple subdomains?

like image 557
marclar Avatar asked Nov 07 '08 21:11

marclar


2 Answers

You tagged this with ASP.NET and IIS, so I will assume that is your environment. Make sure you have this in your web.config:

<httpCookies domain=".usa.com"/>

If your 2 subdomains map to the same application, then you are done. However, if they are different applications you will need to do some additional work, like using a SQL Server based Session storage (and hacking the stored procedures to make sure all applications share the same session data) or with an HttpModule to intercept the application name, since even with shared cookies and the same machine key, 2 applications will still use 2 different stores for their session data.

like image 141
Matt Connolly Avatar answered Oct 19 '22 12:10

Matt Connolly


Track your own sessions and use a cookie with an appropriate domain setting, ie. .usa.com.

Alternatively, if you're using PHP, I believe there's a setting to change the default domain setting of the session cookie it uses, that may be useful too.

The settings you're looking for are:

session.use_cookies = 1
session.use_only_cookies = 1
session.cookie_domain = .usa.com
like image 43
Matthew Scharley Avatar answered Oct 19 '22 12:10

Matthew Scharley