简体   繁体   English

在ASP.NET IIS7中为子域组存储会话cookie

[英]Store Session cookie for groups of subdomains in ASP.NET IIS7

I have an application running ASP.NET. 我有一个运行ASP.NET的应用程序。 I have different domains and different sub-domains. 我有不同的域和不同的子域。 I want the domains to share session with their sub domains. 我希望这些域与其子域共享会话。

For Example, the following domains access this application: 例如,以下域访问此应用程序:
www.example1.com www.example1.com
print.example1.com print.example1.com
www.example2.com www.example2.com
print.example2.com print.example2.com

If a user goes to www.example1.com and print.example1.com, I want it to use the same session. 如果用户访问www.example1.com和print.example1.com,我希望它使用相同的会话。 If the user were to go to www.example2.com and print.example2.com, I would want it to use a different session than the *.example1.com. 如果用户要访问www.example2.com和print.example2.com,我希望它使用与* .example1.com不同的会话。

The way I used to handle it was a hack in page_load that works perfectly in IIS6: 我以前处理它的方式是在IIS6中完美运行的page_load中的hack:
Response.Cookies["ASP.NET_SessionId"].Value = Session.SessionID; Response.Cookies [“ ASP.NET_SessionId”]。Value = Session.SessionID;
Response.Cookies["ASP.NET_SessionId"].Domain = SiteUtility.GetCookieDomain(); Response.Cookies [“ ASP.NET_SessionId”]。Domain = SiteUtility.GetCookieDomain();
(SiteUtility.GetCookieDomain would return .example1.com or .example2.com depending on the url of the request) (SiteUtility.GetCookieDomain将根据请求的网址返回.example1.com或.example2.com)

Unfortunately, this no longer seems to work for iis7. 不幸的是,这似乎不再适用于iis7。 Each subdomain/domain a user goes to, the user gets a new session cookie. 用户访问的每个子域/域都将获得一个新的会话cookie。

I then found the web.config entry: 然后,我找到了web.config条目:
'<httpCookies domain=".example1.com" />. '<httpCookies domain =“。example1.com” />。

This works great for sharing session cookie between example1.com subdomains. 这非常适合在example1.com子域之间共享会话cookie。 Unfortunately, this completely screws up session state for *.example2.com. 不幸的是,这完全破坏了* .example2.com的会话状态。

Any ideas on how I can solve this? 关于如何解决这个问题有什么想法吗?

Have you tried creating a HttpModule that intercepts the EndRequest -event, iterates over the Response.Cookies collection, finds the session-cooke and changes its Domain property before actually sending it to the client. 您是否尝试过创建一个HttpModule来拦截EndRequest -event,遍历Response.Cookies集合,找到会话标记并更改其Domain属性,然后再将其实际发送给客户端。

Edit: 编辑:

Kevin at the end determined that one of the subdomains was in a trusted state in ie8 while the other was not. 凯文最后确定,ie8中的一个子域处于受信任状态,而另一个则不是。 When both are in a trusted state (and presumambly both in an untrusted state) it works. 当两者都处于受信任状态(并且大概都处于不受信任状态)时,它将起作用。 I did spent the most time on this, so Kevin want to give me the credit for the answer. 我确实在此上花费了最多的时间,因此Kevin希望为我提供答案。

rather than using SiteUtility.GetCookieDomain(), try using: 而不是使用SiteUtility.GetCookieDomain(),请尝试使用:

var domainSansTLD = Request.Url.Host.Replace(".com", "");
var secondLevelDomain = domainSansTLD.Substring(domainSansTLD.LastIndexOf('.'));
var cookieDomain = secondLevelDomain + ".com";

First way, 第一种方式

Store session as file in a location that both servers can access (this is good for virtual servers or shared folders). 将会话作为文件存储在两个服务器都可以访问的位置(这对于虚拟服务器或共享文件夹很有用)。 This method is a bit unreliable and shared folders and lag thus causing problems. 此方法有点不可靠,并且共享文件夹和延迟,从而导致问题。

Okay, I was going to write a couple of other methods but I'm sure they'll be posted as they have more knowledge with asp.net than I, however, I do have a personal preference that I think would be a good option. 好的,我打算写一些其他方法,但是我相信它们会被发布,因为他们比我对asp.net拥有更多的知识,但是,我确实有个人偏好,我认为这是一个不错的选择。

This option would be a memcache server. 此选项将是一个内存缓存服务器。 Essentially it's a database that runs off RAM and you point all of your sessions to it (tcp://mem.domain.com:1234 ... sorry I can't remember the usual port off the top of my head). 本质上,这是一个运行在RAM上的数据库,您将所有会话都指向该数据库(tcp://mem.domain.com:1234 ...抱歉,我不记得通常的端口不在我头上了)。

I hope I was of some help, 我希望我能有所帮助,

Jon 乔恩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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