Session sharing between applications using the ASP.NET session state service

I am trying to exchange sessions between two web applications hosted on the same server. One of them is a .net 2.0 application for web forms, the other is a .net 3.5 MVC2 application.

Both applications have their own session configured as follows:

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

In the webform application, I send the session key to the MVC application:

 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 } 

Then I get it in an MVC application and try to use the same session as 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 sent, but the session does not seem to be loaded into the MVC application, i.e. sessionKey is NULL. Can I try to do this?

+44
c # asp.net-mvc session-state
May 19 '10 at 18:37
source share
3 answers

I did it as follows:

The main idea is that both applications use their own .net sessionState stored in sqlserver. Using the same machine key and making a small tweak to the stored procedure, both applications can share any session keys and / or form authentication.

Both applications will do something similar in their web.config:

 <sessionState mode="SQLServer" sqlConnectionString="Data Source=.\SQLEXPRESS;User Id=test;Password=test;Application Name=AppName" /> <machineKey validationKey="SOMEKEY" validation="SHA1" decryption="AES" /> 

The db session state must be configured on the database server that both applications can see.

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

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

The stored procedure (TempGetAppID) is configured to:

  @appId int OUTPUT AS -- start change -- Use the application name specified in the connection for the appname if specified -- This allows us to share session between sites just by making sure they have the -- the same application name in the connection string. DECLARE @connStrAppName nvarchar(50) SET @connStrAppName = APP_NAME() -- .NET SQLClient Data Provider is the default application name for .NET apps IF (@connStrAppName <> '.NET SQLClient Data Provider') SET @appName = @connStrAppName -- end change SET @appName = LOWER(@appName) 
+69
Jun 30 '10 at 16:27
source share

The problem is that session keys are bound to applications, so two applications that have the same session key actually have separate sessions.

You can do one of two things:

  • Place both applications as a virtual directory under the IIS shared application. I do not think this is a good idea, but it will work.

  • Reset your own data transfer solution for the data you want to provide. Perhaps using the base database as a shared repository if you have one that is.

Based on Justin's comment, simply to clarify option 2, it does not refer to the SQL state management system for process sessions. I mean, you are actually manually managing shared data for two sessions, possibly using a database.

+9
May 19 '10 at 18:52
source share

You can use the Machine shared key to create the same session ID in both applications for a given user. In addition, you should also plan to keep the sessions of both applications in a shared repository, such as the ASP.NET public service or distributed cache.

You can use NCache's distributed cache cache, which provides a session sharing feature between different applications. You specify the application tag tag for both applications in the session state settings, which allows you to share the session object if you have the same session identifier generated for both applications.

0
Mar 01 '18 at 5:42
source share



All Articles