Ashley Medway
Ashley Medway

Reputation: 7301

Session State shares session across subdomain, VB.NET website C# MVC 4 Application

I am trying to share a session from a main VB.Net Website and a subdomain C# MVC 4.

I have the same ASP.NET_SessionId Cookie present for both sites and in the Immediate Debugger I can see both Session objects have the same SessionID. However when I set a session from the VB.Net website, Session("TestSession") = "SimpleString", if i call the Session Object in the Immediate Window from the MVC Application the Count is 0.

This is also the case if i set a session in the MVC Application it is not found in the VB.Net Website.

Preferably I would like to use StateServer Mode but I have read that only SQLServer Mode works for cross domain support.

Here is the web.config that is present in both sites

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

Upvotes: 3

Views: 1933

Answers (2)

Ashley Medway
Ashley Medway

Reputation: 7301

To solve the problem as well as adding the web.config settings I had to add a couple of lines to the Application_Start from the global.asax. This had to be added to both sites.

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

Upvotes: 4

Steve
Steve

Reputation: 5545

You should use a cookie and not a session variable. Session variables are saved on the server so when you goto another server, it will not have your session variable.

See this SO question for more detail: where-are-the-session-variables-saved

Upvotes: 1

Related Questions