简体   繁体   中英

asp.net: session clear on response.redirect?

Our site has a DropDownList for country and a DropDownList for language. when a combination of those is clicked, we redirect the user to a country/language specific url, like en-us.mysite.com

the redirect happens in the ButtonClick in a user control (the UserControl is place in a MasterPage) but when the user gets redirected, the session variables are cleared when we check them in the global.asax ( in the request for the newdomain)

this happens both for true and false for the 'responseend' parameter in the Response.Redirect function.

is this by design?

(this is on localhost by the way, with the subdomains specified in the hosts file on my machine)

The session is cleared because the cookie tracking the session is lost, since you are crossing domains.

So yes - it's by design. Not session design, but cookie-design.

But i believe you can allow the cookie to be shared across multiple subdomain's, like so:

Response.Cookies["domain"].Domain = "mysite.com";

Making it available to:

en-us.mysite.com

and

en-au.mysite.com
foo.mystie.com
etc

But that won't solve your problem, since you have no control over the ASP.NET Session cookie.

So you will have to implement your own custom session mechanism, which i haven't done so can't really provide much help with.

As RPM1984 said, it is because you are crossing domains. However, you can resume the same session if you would pass on the session variable and set it once again in the next request, assuming that it would be the same application that handles the second domain.

You could do that in the OnAuthenticateRequest hook and manually set

Request.Cookies.Add(new HttpCookie( "ASP.NET_SessionId", Request.Form["s"] );)

(I don't remeber what the session-cookie is named, or if it is configurable. It should be possible atleast to actually set it somehow)

This assume that you post the session id in a variable called "s" but you can probably figure out a better way of solving it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM