472,378 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

Session Cookie not accessible across Sub-Domains

An ASP.NET session cookie set on "www.mydomain.com" can not be accessed on
"search.mydomain.com"; hence, a new session and cookie are being created on
every sub-domain.

This is occuring because ASP.NET always sets the Session cookie domain to
the full domain (e.g. "www.mydomain.com") instead of the parent domain (e.g.
"mydomain.com")

The problem with this is when the visitor goes to a different sub-domain
(e.g. "search.mydomain.com"), this sub-domain can not access the previously
set Session cookie, and hence, has no idea a session has already been
created. Hence, a new session is created with a new cookie set to
"search.mydomain.com". Now the visitor has two session cookies pointing to
two different sub-domains.

For the past couple of years, I've gotten around this by manually creating a
"ASP.NET_SessionId" cookie pointing to the parent domain (e.g.
"mydomain.com"). That way, all sub-domains have access to the same cookie and
the same session ID. However, this is a hack; I end up with multiple session
cookies pointing to "www.mydomain", "search.mydomain.com", and
"mydomain.com"; not the best solution.

How can I tell ASP.NET to always set the Session cookie domain to
"mydomain.com" so all sub-domains can read it? My research over the past
couple of years tells me this is impossible. This seems to be a major bug
that many people experience, however, I've heard no word of a fix nor any
comment on it from Microsoft.

Doug
Nov 19 '05 #1
7 7662
When initially setting the cookie

Response.Cookies("domain").Value = DateTime.Now.ToString
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "mydomain.com"

.................should do the trick.

I think its case sensitive at the browser.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Doug" <Do**@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
An ASP.NET session cookie set on "www.mydomain.com" can not be accessed on
"search.mydomain.com"; hence, a new session and cookie are being created
on
every sub-domain.

This is occuring because ASP.NET always sets the Session cookie domain to
the full domain (e.g. "www.mydomain.com") instead of the parent domain
(e.g.
"mydomain.com")

The problem with this is when the visitor goes to a different sub-domain
(e.g. "search.mydomain.com"), this sub-domain can not access the
previously
set Session cookie, and hence, has no idea a session has already been
created. Hence, a new session is created with a new cookie set to
"search.mydomain.com". Now the visitor has two session cookies pointing to
two different sub-domains.

For the past couple of years, I've gotten around this by manually creating
a
"ASP.NET_SessionId" cookie pointing to the parent domain (e.g.
"mydomain.com"). That way, all sub-domains have access to the same cookie
and
the same session ID. However, this is a hack; I end up with multiple
session
cookies pointing to "www.mydomain", "search.mydomain.com", and
"mydomain.com"; not the best solution.

How can I tell ASP.NET to always set the Session cookie domain to
"mydomain.com" so all sub-domains can read it? My research over the past
couple of years tells me this is impossible. This seems to be a major bug
that many people experience, however, I've heard no word of a fix nor any
comment on it from Microsoft.

Doug

Nov 19 '05 #2
Hi John,
Thank you for the reply. I'm not sure I understand; or perhaps vice-versa?

I don't set the ASP.NET Session cookie. ASP.NET does that all on it's own. I
do know how to write cookies and set domains, etc. My question is, how do I
get ASP.NET to set the correct domain wherever it set its own cookie?

Thanks,
Doug
"John Timney (ASP.NET MVP)" wrote:
When initially setting the cookie

Response.Cookies("domain").Value = DateTime.Now.ToString
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "mydomain.com"

.................should do the trick.

I think its case sensitive at the browser.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director


Nov 19 '05 #3
sorry I misread your question (its late here!!).

You can't share sessions across domains, nor applications natively - so it
will always set a new cookie as you move between domains. Because you can
share cookies across those applications (and between those domains) one
approach is to store your shared data in a database and use a shared domain
cookie to identify the data in the database.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Doug" <Do**@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
Hi John,
Thank you for the reply. I'm not sure I understand; or perhaps vice-versa?

I don't set the ASP.NET Session cookie. ASP.NET does that all on it's own.
I
do know how to write cookies and set domains, etc. My question is, how do
I
get ASP.NET to set the correct domain wherever it set its own cookie?

Thanks,
Doug
"John Timney (ASP.NET MVP)" wrote:
When initially setting the cookie

Response.Cookies("domain").Value = DateTime.Now.ToString
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "mydomain.com"

.................should do the trick.

I think its case sensitive at the browser.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Nov 19 '05 #4
Hi John,
I wasn't referring to sharing sessions across parent domains (e.g.
"mydomain1.com" and "mydomain2.com"). I want to share sessions on sub-domains
of the same domain (e.g. "www.mydomain.com" and "search.mydomain.com").
Regards,
Doug
"John Timney (ASP.NET MVP)" wrote:
sorry I misread your question (its late here!!).

You can't share sessions across domains, nor applications natively - so it
will always set a new cookie as you move between domains. Because you can
share cookies across those applications (and between those domains) one
approach is to store your shared data in a database and use a shared domain
cookie to identify the data in the database.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Nov 19 '05 #5
I expect the problem would be the same. Asp.net bounds sessions and objects
within applications for security, so if your subdomains were not part of the
same web application then the session would not apply. The solution could
be to have a root application, with all your other applications hanging
under it as non application virtual directories - and then have something
like the isapi virtual hosting filter handle the domains, allowing the root
application to own the single session. I've never tried it myself though.
I would always see a sub-domain as a seperate application entirely, or why
would it be a sub-domain?

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Doug" <Do**@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
Hi John,
I wasn't referring to sharing sessions across parent domains (e.g.
"mydomain1.com" and "mydomain2.com"). I want to share sessions on
sub-domains
of the same domain (e.g. "www.mydomain.com" and "search.mydomain.com").
Regards,
Doug
"John Timney (ASP.NET MVP)" wrote:
sorry I misread your question (its late here!!).

You can't share sessions across domains, nor applications natively - so
it
will always set a new cookie as you move between domains. Because you
can
share cookies across those applications (and between those domains) one
approach is to store your shared data in a database and use a shared
domain
cookie to identify the data in the database.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Nov 19 '05 #6
Well, the out-of-proc StateServer works just fine for sharing sessions across
sub-domains. Everything in ASP.NET allows for sharing sessions across
sub-domains; everything except this simple cookie issue.

Let me explain one of the reasons why I need sessions to be shared across
sub-domains:
I have a "www" server, and a "search" server. When a person signs in, the
HTML header at the top of every page shows a link to "Sign Out". This same
header is used on every page throughout the site; on both "www" and "search".
Based on the session, I know whether the person is signed in or not, and
whether to show the "Sign Out" link or not. The session needs to persist
across sub-domains; otherwise, when a person goes to the "search" server,
they wouldn't appear to be signed in any longer.

There are many real-world examples of why sessions need to be shared across
sub-domains. e.g. Yahoo uses a single sign-on and you stay signed-in across
"mail.yessy.com", "shopping.yahoo.com", "music.yahoo.com", etc.

There are just so many examples of why a session would need to be shared
across sub-domains.

The ASP.NET StateServer natively supports sub-domains. The only issue is the
domain setting for the Session cookie. Instead of tying the cookie to
"www.mydomain.com", allow the cookie to be tied to "mydomain.com". That way,
all sub-domains can access the cookie and problem solved. People stay
signed-in across sub-domains; the same session can be accessed; etc.

Why not allow developers to share sessions across sub-domains if they need
to? It's an extremely simple feature to provide.

By the way, I implemented a fairly good fix/hack today. Put this code on
every page:
Response.Cookies["ASP.NET_SessionId"].Value = Session.SessionID;
Response.Cookies["ASP.NET_SessionId"].Domain = ".mydomain.com";

Those two lines of code rewrite the Session cookie so it's now accessible
across sub-domains.

My hope is that Microsoft will implement a web/machine.config param that
allows the Session cookie to be accessed across sub-domains.

Doug

"John Timney (ASP.NET MVP)" wrote:
I expect the problem would be the same. Asp.net bounds sessions and objects
within applications for security, so if your subdomains were not part of the
same web application then the session would not apply. The solution could
be to have a root application, with all your other applications hanging
under it as non application virtual directories - and then have something
like the isapi virtual hosting filter handle the domains, allowing the root
application to own the single session. I've never tried it myself though.
I would always see a sub-domain as a seperate application entirely, or why
would it be a sub-domain?

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Doug" <Do**@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
Hi John,
I wasn't referring to sharing sessions across parent domains (e.g.
"mydomain1.com" and "mydomain2.com"). I want to share sessions on
sub-domains
of the same domain (e.g. "www.mydomain.com" and "search.mydomain.com").
Regards,
Doug
"John Timney (ASP.NET MVP)" wrote:
sorry I misread your question (its late here!!).

You can't share sessions across domains, nor applications natively - so
it
will always set a new cookie as you move between domains. Because you
can
share cookies across those applications (and between those domains) one
approach is to store your shared data in a database and use a shared
domain
cookie to identify the data in the database.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director


Nov 19 '05 #7
good hack - I'll remember that one :)

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Doug" <Do**@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
Well, the out-of-proc StateServer works just fine for sharing sessions
across
sub-domains. Everything in ASP.NET allows for sharing sessions across
sub-domains; everything except this simple cookie issue.

Let me explain one of the reasons why I need sessions to be shared across
sub-domains:
I have a "www" server, and a "search" server. When a person signs in, the
HTML header at the top of every page shows a link to "Sign Out". This same
header is used on every page throughout the site; on both "www" and
"search".
Based on the session, I know whether the person is signed in or not, and
whether to show the "Sign Out" link or not. The session needs to persist
across sub-domains; otherwise, when a person goes to the "search" server,
they wouldn't appear to be signed in any longer.

There are many real-world examples of why sessions need to be shared
across
sub-domains. e.g. Yahoo uses a single sign-on and you stay signed-in
across
"mail.yessy.com", "shopping.yahoo.com", "music.yahoo.com", etc.

There are just so many examples of why a session would need to be shared
across sub-domains.

The ASP.NET StateServer natively supports sub-domains. The only issue is
the
domain setting for the Session cookie. Instead of tying the cookie to
"www.mydomain.com", allow the cookie to be tied to "mydomain.com". That
way,
all sub-domains can access the cookie and problem solved. People stay
signed-in across sub-domains; the same session can be accessed; etc.

Why not allow developers to share sessions across sub-domains if they need
to? It's an extremely simple feature to provide.

By the way, I implemented a fairly good fix/hack today. Put this code on
every page:
Response.Cookies["ASP.NET_SessionId"].Value = Session.SessionID;
Response.Cookies["ASP.NET_SessionId"].Domain = ".mydomain.com";

Those two lines of code rewrite the Session cookie so it's now accessible
across sub-domains.

My hope is that Microsoft will implement a web/machine.config param that
allows the Session cookie to be accessed across sub-domains.

Doug

"John Timney (ASP.NET MVP)" wrote:
I expect the problem would be the same. Asp.net bounds sessions and
objects
within applications for security, so if your subdomains were not part of
the
same web application then the session would not apply. The solution
could
be to have a root application, with all your other applications hanging
under it as non application virtual directories - and then have something
like the isapi virtual hosting filter handle the domains, allowing the
root
application to own the single session. I've never tried it myself
though.
I would always see a sub-domain as a seperate application entirely, or
why
would it be a sub-domain?

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Doug" <Do**@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
> Hi John,
> I wasn't referring to sharing sessions across parent domains (e.g.
> "mydomain1.com" and "mydomain2.com"). I want to share sessions on
> sub-domains
> of the same domain (e.g. "www.mydomain.com" and "search.mydomain.com").
> Regards,
> Doug
>
>
> "John Timney (ASP.NET MVP)" wrote:
>
>> sorry I misread your question (its late here!!).
>>
>> You can't share sessions across domains, nor applications natively -
>> so
>> it
>> will always set a new cookie as you move between domains. Because you
>> can
>> share cookies across those applications (and between those domains)
>> one
>> approach is to store your shared data in a database and use a shared
>> domain
>> cookie to identify the data in the database.
>>
>> --
>> Regards
>>
>> John Timney
>> ASP.NET MVP
>> Microsoft Regional Director
>>


Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: A Web Master | last post by:
I am designing a site for a client where I have a frameset and 3 frames (all in ASP). I am creating session variables in the frameset that need to be accessed in the frames. It seams that in...
1
by: Matt | last post by:
I want to know what's the differences between session cookie and regular cookie. In ASP, when we create cookie, we do the following to identify an user: Response.Cookies("name") = value Is...
3
by: Karsten Grombach | last post by:
Hi, I'm trying the following: - Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https) - On success redirect to the page (encapsuled in an iframe)...
14
by: Schoo | last post by:
I have an asp.net app that uses session objects (ag. session("UserID")). The app works fine in development/debug mode. I released it to the test server (Windows 2000 server with other .NET...
8
by: ari | last post by:
hey all, i'm trying to make my app as stateless as possible. is it ok to create a dataset and store in viewstate and whenever the user decides to select a from that dataset, to move from...
9
by: McGeeky | last post by:
Is there a way to get a user control to remember its state across pages? I have a standard page layout I use with a header and footer as user controls. Each page uses the same layout by means of...
0
by: joseph conrad | last post by:
Hi, I tried to implement my own session handler in order to keep control on the process the drawback I foun it is not creating and storing in my cookie the PHPSESSID variable anymore. reading te...
4
by: mike.biang | last post by:
I have an ASP page that is using an XMLHTTP object to request various pages from my server. I keep a single session throughout the XMLHTTP requests by bassing the ASPSESSIONID cookie through the...
4
by: rgparkins | last post by:
Hello I am running out of time with a problem I have running PHP 5.04 and Apache 2.0 and really need help :(. I have a page that stores a variable in session but each time I reload that page the...
9
by: Josh | last post by:
I run a Joomla website and am familiar with php in some but not all aspects. Currently I am trying to find some solutions related to session handling. Am I correct in saying that "login" is kept...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.