2

我正在尝试从主 VB.Net 网站和子域 C# MVC 4 共享会话。

我为两个站点提供了相同的 ASP.NET_SessionId Cookie,在即时调试器中我可以看到两个 Session 对象具有相同的 SessionID。但是,当我从 VB.Net 网站设置会话时Session("TestSession") = "SimpleString",如果我从 MVC 应用程序调用即时窗口中的会话对象,则计数为 0。

如果我在 MVC 应用程序中设置会话,在 VB.Net 网站中找不到它,情况也是如此。

最好我想使用 StateServer 模式,但我读过只有 SQLServer 模式适用于跨域支持。

这是两个站点中都存在的 web.config

<httpCookies domain=".example.localhost"/>
<sessionState mode="SQLServer" sqlConnectionString="Data Source=(local);Integrated Security=True" />
<machineKey validationKey="XXX" decryptionKey="XXX" validation="SHA1" compatibilityMode="Framework20SP1" />
4

2 回答 2

4

为了解决这个问题以及添加 web.config 设置,我必须从 global.asax 向 Application_Start 添加几行。这必须添加到两个站点。

C#

FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
appNameInfo.SetValue(theRuntime, "MyApplicationName");

VB.Net

Dim runtimeInfo As System.Reflection.FieldInfo = GetType(HttpRuntime).GetField("_theRuntime", System.Reflection.BindingFlags.[Static] Or System.Reflection.BindingFlags.NonPublic)
Dim theRuntime As HttpRuntime = DirectCast(runtimeInfo.GetValue(Nothing), HttpRuntime)
Dim appNameInfo As System.Reflection.FieldInfo = GetType(HttpRuntime).GetField("_appDomainAppId", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
appNameInfo.SetValue(theRuntime, "MyApplicationName")
于 2013-10-23T15:41:21.483 回答
1

您应该使用 cookie 而不是会话变量。会话变量保存在服务器上,因此当您转到另一台服务器时,它不会有您的会话变量。

有关更多详细信息,请参阅此 SO 问题:where-are-the-session-variables-saved

于 2013-10-23T14:48:50.963 回答