A Coder's Blog
In a distributed environment using the SQL Server cache implementation we can store session values in SQL server.
For more info about distributed caching in asp.net core: distributed caching in asp.net core
note: you need to have .net sdk 2.1 installed.
you can run the following command using command prompt to create table and index for your new session table (this will create a TestSession table under Test DB)
I’m going to create a new login/user (security -> new login). I will need this user in order to read and write in the new Session Table.
here I’m giving to the new user the permission to select/update/write/delete on the TestSession table.
Create a new project (ASP.NET core Web Application) selecting NET.Core 2.1 as target Framework and ASP.NET MVC as project template.
Using Nuget install the following packages: Microsoft.Extensions.Caching.SqlServer, Microsoft.Extensions.Caching.SqlConfig.Tools,Microsoft.AspNetCore.Session
In the appsettings.json add the connection string:
better not to add the Password, otherwise it will end up in your version control repository.
You can use secret manager to store the password:
in the secret.json insert the password
In the startup.cs, in the ConfigureServices method add the Distributed Sql Server Cache:
In the Configure method insert app.UseSession();
to test it in any action of a controller save and retrieve a session value
you should be able to see now a new entry in the TestSession table
To store and retrieve complex objects you need to serialize and deserialize, you can use JsonConvert class for example.