ASP.NET login to subdomain from parent domain

2019-07-03 08:34发布

问题:

I have an ASP.Net MVC site that has a subdomain for each customer e.g. customer1.site.com, customer2.site.com, etc.

Login works fine from customer1.site.com/login and customer2.site.com/login using the standard ASP.Net FormsAuthentication.

How can I login from the parent domain (e.g. site.com/login) where the user specifies the subdomain name in a form field? I'd like the auth cookie to be stored against customer1.site.com or customer2.site.com so obviously need to redirect and repost the login form somehow.

回答1:

you need to set the forms auth cookie domain to ".site.com" (note the leading .)

see here for setting the cookie domain: http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.cookiedomain(v=VS.100).aspx



回答2:

I ended up solving this by splitting this into two separate pages. On the first page the user can enter the subdomain name (e.g. customer1) only in a form, on submitting the form they are redirected to the subdomain login page (e.g. customer1.site.com/login).