Share session and sessionID across subdomain (MVC 3)
I tried desperately to find a solution for these two days, but all the tracks have not been conclusive.
I have a website with subdomains like A.mysite.com and B.mysite.com, And I need to share the session between all the subdomain and the domain.
I precise, the issue is not to have persistance across differents servers but in the same application keep the session alive between subdomains.
开发者_JAVA技巧Any ideas will be really appreciated.
You can leverage the build in Session State Server facilities for this.
What this does is allow you to point the web sites to a sql server database in order to persist your session state as opposed to memory. All servers can utilize the same facility seamlessly to your a websites by using session just like you do normally.
There is only a web.config file change, and using aspnet_regsql.exe tool to set up the database facilities.
To set up the database with aspnet_regsql.exe the command line args are as followed;
-S Species the IP address or the name of SQL server in which you want to store the session state.
-U Specifies the user ID to be used when connecting to the SQL Server
-P Specifies the password to be used when connecting to the SQL Server
-E Indicates that you want to use integrated security when connecting to the SQL Server
-ssadd Adds support for the SQLServer mode session state
-ssremove Removes support for the SQLServer mode session state
-sstype Type of session state support. This option can be:
t for temporary storage
p for persistent storage
c for custom storage
-d The name of the custom database to use if -sstype switch is "c"
You would update your web.config file with the following syntax;
<sessionState
mode="SQLServer"
sqlConnectionString="data source=MyDbServer;user id=<username>;password=<strongpassword>"
cookieless="false"
timeout="20"
/>
Here is a couple of good articles on how to accomplish this.
http://idunno.org/articles/277.aspx
http://support.microsoft.com/kb/317604
http://www.developer.com/net/asp/article.php/3595766/Storing-Session-State-in-a-SQL-Server-Database.htm
Since your main problem is assuring that the SessionID is the same in both subdomains, I think your in here is the global Application_PreRequestHandlerExecute event. You can check for the existence of the Session cookie from subdomain A, and set the ID in subdomain for B to the same ID. See post here; http://www.know24.net/blog/ASPNET+Session+State+Cookies+And+Subdomains.aspx
精彩评论