2013-10-23 34 views
2

我想分享一個主要VB.Net網站和子站點,C#MVC 4會話國有股會話,VB.NET網站C#MVC 4應用

我有同樣的ASP.NET_SessionId會話對於這兩個站點和立即調試器均提供Cookie我可以看到兩個Session對象具有相同的SessionID。但是,當我從VB.Net網站設置會話Session("TestSession") = "SimpleString",如果我從MVC應用程序調用立即窗口中的會話對象計數爲0.

這也是這種情況,如果我設置會話中的MVC應用程序它不是在VB.Net網站上找到的。

最好我想使用StateServer模式,但我讀了只有SQLServer模式適用於跨域支持。

這裏是一個存在於兩個站點

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

回答

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") 
+0

如果應用程序位於不同的子域,這將不起作用。 –

+0

@MichaelFry這是部署到不同的子域,並正常工作。 –

+1

你說得對,它確實有效。不知道什麼是錯誤的......哦,歡呼! –

1

您應該使用一個Cookie,而不是一個會話變量web.config中。會話變量保存在服務器上,所以當你轉到另一臺服務器時,它不會有你的會話變量。

參見這太問題的更多詳細信息:where-are-the-session-variables-saved

+0

你是不正確的,你可以使用的sessionState創建的網站連接到他們的會話一個會話服務器作爲...它發生我已經解決了這個問題,它的工作 –

+0

@AshleyMedway如果你已經解決了這個問題,你應該把它作爲答案,然後將其標記爲已回答。這樣,有同樣問題的其他人可以找到他們的答案。 – Steve