会话状态跨子域共享会话,VB.NET网站C#MVC 4应用程序

时间:2013-10-23 10:28:31

标签: c# asp.net asp.net-mvc vb.net session

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

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

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

最好我想使用StateServer Mode,但我读过只有SQLServer Mode才能用于跨域支持。

这是两个站点中都存在的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" />

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:1)

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

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

相关问题