如何在 ASP.NET 中跨多个子域共享会话?

如何在 ASP.NET 中跨多个子域共享会话?

问题描述:

我有一个应用程序,在使用该应用程序的过程中,用户可能会点击来自

I have an application where, in the course of using the application, a user might click from

virginia.usa.com

newyork.usa.com

因为我不想每次用户从一个子域跨到另一个子域时都创建一个新会话,那么跨多个子域共享会话信息的好方法是什么?

Since I'd rather not create a new session each time a user crosses from one subdomain to another, what's a good way to share session info across multiple subdomains?

你用 ASP.NET 和 IIS 标记了这个,所以我假设这是你的环境.确保你的 web.config 中有这个:

You tagged this with ASP.NET and IIS, so I will assume that is your environment. Make sure you have this in your web.config:

<httpCookies domain=".usa.com"/>

如果您的 2 个子域映射到同一个应用程序,那么您就完成了.但是,如果它们是不同的应用程序,您将需要做一些额外的工作,例如使用基于 SQL Server 的会话存储(并破解存储过程以确保所有应用程序共享相同的会话数据)或使用 HttpModule 来拦截应用程序名称,因为即使使用共享 cookie 和相同的机器密钥,2 个应用程序仍将使用 2 个不同的存储来存储它们的会话数据.

If your 2 subdomains map to the same application, then you are done. However, if they are different applications you will need to do some additional work, like using a SQL Server based Session storage (and hacking the stored procedures to make sure all applications share the same session data) or with an HttpModule to intercept the application name, since even with shared cookies and the same machine key, 2 applications will still use 2 different stores for their session data.