I have a web application that can be installed on many domains and paths.
So:
- client1Name. {MySite.com}
- client2Name. {MySite.com}
- demo. {MySite.com} / prospect1Name
- demo. {MySite.com} / prospect2Name
- demo. {MySite.com} / prospect3Name
All individual application instances of the same code.
The problem is if the client is in client1Name. {mySite.com}, then he visits one of the other sites, their browser sends an authentication cookie.
In all cases, FormsAuthentication.SetAuthCookie does not set either Path or Domain .
What would I expect:
- client1Name. {mySite.com} -
Domain = client1Name. {mySite.com} Path = / - client2Name. {mySite.com} -
Domain = client2Name. {mySite.com} Path = / - demo. {mySite.com} / perspective1Name -
Domain = demo. {mySite.com} Path = / perspective1Name - demo. {mySite.com} / perspective2Name -
Domain = demo. {mySite.com} Path = / perspective2Name - demo. {mySite.com} / perspective3Name -
Domain = demo. {mySite.com} Path = / perspective3Name
I can manually override .Net behavior to explicitly set them, but I'm not sure why I need it - this should be the default behavior when setting a cookie for authentication, or at least an option that can be set without re writing big pieces.
Am I missing something? Is there a way to make FormsAuthentication.SetAuthCookie set Path and Domain ?
If not the best way to dynamically read the best Path and Domain ? The same code should work on all sites, and I do not want to add an additional configuration key.
Update
Here is my current solution:
// replacement for FormsAuthentication.SetAuthCookie(user.UserName, false); // as that fails to limit the cookie by domain & path and fails. var cookie = FormsAuthentication.GetAuthCookie(username, false); cookie.HttpOnly = true; cookie.Path = this.Request.ApplicationPath; cookie.Secure = string.Equals("https", this.Request.Url.Scheme, StringComparison.OrdinalIgnoreCase); // the browser will ignore the cookie if there are fewer than two dots // see cookie spec - http://curl.haxx.se/rfc/cookie_spec.html if (this.Request.Url.Host.Split('.').Length > 2) { // by default the domain will be the host, so www.site.com will get site.com // this may be a problem if we have clientA.site.com and clientB.site.com // the following line will force the full domain name cookie.Domain = this.Request.Url.Host; } this.Response.Cookies.Add(cookie);
However, this seems like a lot of workaround for something that FormsAuthentication.SetAuthCookie should be able to do. Is this really the best way?
Keith
source share