如何保持整个ASP.NET中的多个Web应用程序相同的会话ID [英] How to maintain the same session id across multiple web applications in ASP.NET
问题描述
我有不同的虚拟目录在IIS上两个相同的应用程序设置(我做了一些解决办法,以确保它们都具有相同的应用程序名称)。是否有跨两个asp.net web应用程序共享会话ID的方法吗?
I have two identical applications setup on IIS on different virtual directories (I have done some workaround to ensure that they both have the same application name). Is there a way to share session id across two asp.net web applications?
由于我存储的StateServer会话,他们都应该得到同样的会话数据,但是,不同的会话ID创建每次我从应用程序到applicatino湾这是不是发生在一个负载均衡的情况呢?在那里,当我去www.test.com,它会重定向请求服务器,然后如果我再次打它,它会去到服务器B,但因为它是一个不同的Web应用程序,它会创建一个新的会话ID ?
Since I'm storing the session in StateServer, they should both be getting the same session data, however, a different session id is created everytime I go from application a to applicatino b. Wouldn't this happen in a load balancing scenario as well? Where when I go to www.test.com, it would redirect that request to server a, and then if I hit it again, it would go to server b, but since it's a different web application, it would create a new session id?
推荐答案
首先,在你的web.config在这两个应用程序使用cookieName =SOME_COOKIE_NAME_HERE配置sessionState元素。
First, configure the sessionState element in your web.config to use cookieName="SOME_COOKIE_NAME_HERE" in both apps.
然后,只需确保网址具有相同的TLD(顶级域名),即app1.mydomain.com和app2.mydomain.com,你应该能够处理在Global.asax中在session_start事件,并把这个code:
Then, just make sure the urls have the same TLD (top-level domain), i.e. app1.mydomain.com and app2.mydomain.com and you should be able to handle the Session_Start event in Global.asax and put this code:
HttpCookie cookie = new HttpCookie("SOME_COOKIE_NAME_HERE", Session.SessionID.ToString());
cookie.Expires = DateTime.Now.AddMinutes(20);
cookie.Domain = "*.mydomain.com";
cookie.HttpOnly = true;
Response.SetCookie(cookie);
另外,我建议你去与SqlServer的SessionState会模式。
Also, I would recommend that you go with the SqlServer SessionState Mode.
这篇关于如何保持整个ASP.NET中的多个Web应用程序相同的会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!