Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing Session State with ASP.NET/SQL Server

I have a ASP.NET MVC workflow configured as two websites managed by a load balancer. The websites use Sql Server as the session state provider and have authentication switch off (its not required).

Now, sporadically I appear to be losing session state, and I believe this is because the request is being handled by the alternative server, so essentially the user is jumping from server to server, depending how the load balancer sees fit. I am not always "losing session state" at the same stage in the workflow, so I believe it is something related to the web farm configuration + sql server session state.

Both applications use the same machine key to encrypt and decrypt session state stored in sql server.

The configuration on both servers is as follows:

<authentication mode="None" />
<sessionState mode="SQLServer" sqlConnectionString="{connection-string}" />
<machineKey decryptionKey="777CB456774AF02F7F1AC8570FAF31545B156354D9E2DAAD" 
            validationKey="89B5B536D5D17B8FE6A53CBB3CA8B8695289BA3DF0B1370BC47D362D375CF91525DDB5307D8A288230DCD4B3931D23AED4E223955C45CFF2AF66BCC422EC7ECD" />

I've confirmed that this is identical on both servers, is there something I am missing?

This does not occur in my development environment when I am using a single server.

I fear I am suffering from the Friday blues, and no doubt will figure out the answer next week, sadly I don't want to wait!

Any ideas?

like image 265
Matthew Abbott Avatar asked May 11 '12 16:05

Matthew Abbott


People also ask

How can store session data in ASP.NET state server?

Custom mode specifies that you want to store session state data using a custom session state store provider. When you configure your ASP.NET application with a Mode of Custom, you must specify the type of the session state store provider using the providers sub-element of the sessionState configuration element.

How does SQLServer session state works?

The session state is stored in the ASPState database. The advantage of this method is that the data is persisted even if you restart the SQL server. Custom storage: Both the session state data and the stored procedures are stored in a custom database. The database name must be specified in the configuration file.

Where is Inproc session stored?

The session data is stored in the application domain of the web server.


3 Answers

Found the issue.

When you create applications that you expect to share session state using Sql Server, they need the same ID configured in IIS. This is because the session ID that is generated is generated based on the application ID. (Internally the application ID is something like LM/W3SVC/1

The two servers had different IDs for each application in IIS. The resolution is to change the ID under `Manage Website -> Advanced Settings' on each server.

enter image description here

like image 65
Matthew Abbott Avatar answered Sep 19 '22 23:09

Matthew Abbott


  1. Using IIS Manager, double-check your machine key settings at both the root and website levels.

enter image description here

  1. Review the IsolateApps modifier on the machine key element - http://msdn.microsoft.com/en-us/library/w8h3skw9%28v=vs.100%29.aspx

  2. Did you recently upgrade from 3.5 to 4.0?

  3. Last resort - recycle the appPools on both machines, and restart IIS.

like image 30
Kris Krause Avatar answered Sep 22 '22 23:09

Kris Krause


Sessions have (web) application scope. See if this MS KB helps.

Update: Interesting down vote. Perhaps my suggestion needs more clarity.

In addition to matching machine keys, you also have to match the IIS configuration for sites (so it's the "same application" (application path) in IIS.

like image 39
EdSF Avatar answered Sep 20 '22 23:09

EdSF