views:

502

answers:

2

I am trying to share sessions between two web applications, both hosted on the same server. One is a .net 2.0 web forms application the other is as .net 3.5 MVC2 application.

Both apps have their session set up like this:

<sessionState
      mode="StateServer"
      stateConnectionString="tcpip=127.0.0.1:42424"
      />

In the webform application I am posting the the session key to the MVC app:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Session["myvariable"] = "dan"; 
    string sessionKey = HttpContext.Current.Session.SessionID;

    //Followed by some code that posts sessionKey to the other application    
}

I then recieve it in the MVC application and try use the same session like this:

[HttpPost]
public  void Recieve(string sessionKey )
{
    var manager = new SessionIDManager();

    bool redirected;
    bool IsAdded;

     manager.SaveSessionID(HttpContext.ApplicationInstance.Context, Id, out redirected, out IsAdded);
     var myVar = Session["myvariable"];

}

The key is being posted but the session does not seem to get loaded in the MVC app, i.e. sessionKey is null. Can what I am trying to do be done?

+1  A: 

The problem is that session keys are scoped to the applications, so two applications having the same session key in fact have separate sessions.

You can do one of two things.

1- Put both applications as a virtual directory under a common IIS Application. I don't think this is a good idea, but it will work.

2- Roll your own session data solution for the data you want to share. Possibly using the backend database as the common storage, if you have one that is.

Based on Justin's comment, just to clarify option 2 is not refering to the SQL state managemet for out of process sessions. I mean for you to actually manually manage the shared data for the two sessions, possibly using a database.

Chris Taylor
I was going to suggest using the database for session management solution as well. It's very easy to setup but you might run into the same problem different apps will have different keys and can't share data.
Justin
@Justin, I am not suggesting that the OP use the ASP.NET session in SQL Server. This will definately have the same issue, session keys in ASP.NET are scoped to the Application, regardless of the persistence. I mean the OP should entirely manage the storage of the shared data "manually" and only use the ASP.NET session mechanism for the application specific session data if required.
Chris Taylor
Why is point 1 a bad idea?
Dan
@Dan, the thing is that you are mixing two applications into one scope. You now not only share session data, but but have both application loaded into the same appdomain, where static data, application variables, cache variable all potentially impact each other, just because you wanted to share session data.
Chris Taylor
Makes sense, thanks for explaining.
Dan
A: 

I did it this way:

Basically the idea is both apps use native .net sessionState stored in sqlserver. By using the same machine key and making a small tweak to a stored procedure – both apps can share any session keys and/or forms authenication.

Both apps would do something like this in there web.config:

Session state db would need to be set up on a database server, that both apps can see.

Docs for doing this: http://msdn.microsoft.com/en-us/library/ms229862(VS.80).aspx

Command that would need to be run: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin>aspnet_regsql.exe -E -ssadd --sstype p -S .\SQLEXPRESS

Stored procedure tweak to: http://www.sneal.net/blog/2007/06/27/SharingSessionBetweenWebAppsViaConfiguration.aspx

Dan