Step by Step – Enable ASP.NET Session State on SharePoint 2010 Application

ASP.NET Session state is disabled by default in SharePoint 2010 installation. Although it may not require in typical SharePoint 2010 installations, there may be need in custom solutions & web parts built on top of SharePoint 2010 framework where developers required persisting information per user session.

How ASP.NET Session State really works in SharePoint?

As many of you are aware, ASP.NET allows persisting session states in three different medium – on server memory as a inproc which would require server & load balancer affinity, on SQL Server to persist session in database, or on Session State Server to persist session on dedicated server memory. Both SQL Server and Session State Server would support server farm environment and because SharePoint is the multi-server farm environment based on ASP.NET framework, by default, it can be enabled to use SQL Server and SqlSessionStateStore provider to persist session state using ASP.NET Session State Service Application.

Should I really worry if ASP.NET Session State is enabled on my SharePoint farm?

ASP.NET Session State Service as SharePoint Session State Management depends on many factors including how many concurrent users will be accessing site, or heavy vs. average vs. low usage of session objects. If you really need to persist user session information and if you can store them in other mediums like cookies, ASP.NET Session state may not be necessary. Since SharePoint is built on top of ASP.NET framework, ASP.NET Session State Service would be ideal place to persist user sessions. Since ASP.NET Sessions gets stored in the SQL Server, heavy usage of session state may require detailed database planning, regular truncating/maintenance/cleanup of expired session state data, or fine tuning of default 60 minute session expiration time. Great thing about this service is even though it’s enabled on whole farm, it’s not used unless specific application is activated to use session state and you have full control & visibility of how ASP.NET sessions gets stored in SharePoint.

How do I enable ASP.NET Session State in SharePoint?

ASP.NET Session state is available to the SharePoint environment by enabling “SharePoint Server ASP.NET Session State Service” service application on the SharePoint farm. You can’t enable this service application using central administration browser interface. You must enable it using PowerShell command.

Step 1: Enable ASP.NET Session State Service

To enable ASP.NET session state, log on the Central Admin Server using Farm Admin Account, and

  • Either Run PowerShell command “Enable-SPSessionStateService –DefaultProvision” to create service application with default state. By default, this will create service application database with “SessionStateService_<GUID>”, on the same database server where farm configuration database is located using windows credentials of the logged in user.
  • Or Run PowerShell command “Enable-SPSessionStateService -DatabaseServer YourDBServerName -DatabaseName YourDBName” to create service application with specific database name on non-SharePoint configuration database server. For more details and additional parameters, please visit Enable-SPSessionStateService

By enabling Session State Service on your farm,

It would create database on specified server – SessionStateService_<GUID>

It would create SharePoint Server ASP.NET Session State Service in Manage Service Applications

It would add module in all web applications on farm – <add name=”Session” type=”System.Web.SessionState.SessionStateModule” />

It would add sessionstate entry in all web applications on farm

<sessionState mode=”SQLServer” timeout=”60″ allowCustomSqlDatabase=”true” sqlConnectionString=”Data Source=SP2010VM;Initial Catalog=SessionStateService_1079ab25364440b0b38b15ad2392b6d0;Integrated Security=True;Enlist=False;Connect Timeout=15″ />

Step 2: Activate ASP.NET Session State on SharePoint Web Application

By enabling ASP.NET session state service in the SharePoint farm, every SharePoint web application in the farm ready to use ASP.NET session framework. To ensure SharePoint web application gets activated to persist ASP.NET sessions, you have to manually update Web.Config file for the specific SharePoint web application on all servers in the farm.  – <pages enableSessionState=”true”

Step 3: Although this may not be necessary, performing IISReset on all the servers would help resetting all the sessions.

How would you use ASP.NET Session State Service in Code?

This is straightforward. You can access ASP.NET session state object using HTTPConext.Current.Session and use the ASP.NET session state same as typically ASP.NET Session Management. Here is the sample code which would create new ASP.NET session object called “SampleSessionKey”, if it doesn’t exists.

After above code runs, this is what you would see in the ASP.NET Session State Service Application database. By default, it would create session for 60 minutes. If you want to increase or decrease session expiration, you have to pass “SessionTimeout” parameter while creating ASP.NET Session State Service using Enable-SPSessionStateService command.

Additional References

This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Step by Step – Enable ASP.NET Session State on SharePoint 2010 Application

  1. Pingback: sessionstate « venkateshprogramer

Comments are closed.