How To Configure SQL Server Session State In ASP.NET Core

How To Configure SQL Server Session State In ASP.NET Core

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core hosting. Session is a feature in ASP.NET Core that enables us to save/store user data. Session stores the data in a dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at the cookie. The SessionId cookie is sent with every request. The SessionId cookie is Browser specific  and it cannot be shared among the Browsers. There is no timeout specified for SessionId cookie and they are deleted when the Browser session ends.

Session are of two types: InProc or In-memory and OutProc or Distributed session. If our session is in-memory and our Application is hosted on Web-Farm environment, we need to use sticky sessions to tie each session to a specific server. Whereas OutProc session does not require sticky sessions and they are the most preferred way to use session in our application.

Configure SQL Server session state

In SQL Server Session state, previous version of ASP.NET requires a number of tables and stored procedures to manage the storage of session in SQL server and this can be configured, using “aspnet_regsql” command or a tool. ASP.NET Core requires only one table. There are multiple ways to generate this storage table. The first is to use “Microsoft.Extensions.Caching.SqlConfig.Tools” tool. Before we run the commands of these tools, we need to install this tool. To install this tool, we need to add this tool in project.json and perform “dotnet restore” command.
Once this tool is installed for our project, we can execute “sql-cache” command, which generates the required table and an index. The command is in the format given below.
 Here, I am using SQL Express edition and following becomes full command, which generates the table and an index.

 cd

sas

Alternatively, we can create this table and an index manually. The script is given below to create a table and an index.

 SQL session state internally uses memory cache, so as to enable SQL Server session. We need to add “Microsoft.Extensions.Caching.SqlServer” dependency along with “Microsoft.AspNet.Session” into project.json file.

We can add a connection string to appSetting.json file or put it directly in ConfigureServices method of startup class. Following is the definition of ConfigureServices and Configure method of startup class.

Example

In the example given below, I have set my name into session in the first request and retrieved the session value in another request.

 ww

With SQL server state in ASP.NET Core, we can also create and use the session extension methods and we can also store complex objects in to the session.

Summary

This article described the steps required to enable SQL Server as a storage mechanism for session state in an ASP.NET Core MVC Application. The usage of SQL server session state is slightly different than the classic ASP.NET Application.