Introduction
I am assuming that you already have created default project for ASP.NET core MVC app using visual studio.
project.json
Create a new file at root of project and name is project.json and copy below content inside it.
Run dotnet restore command in package manager console.
Create database and table to store session
Now in your controller files you can use Session Class methods to save and retrieve session values from distributed Cache.
Save data to session
In this article, I am going to explain what are simple steps if you want to use distributed sessions in order to make your ASP.NET Core application load balanced.
Assumption
Assumption
I am assuming that you already have created default project for ASP.NET core MVC app using visual studio.
Sessions
Session object is used in order to save or store user data for configurable amount of time. It uses dictionary on server side to store data and session id can be used as key to manage data back and forth.
There are two types of sessions:
1. In Memory
2. Distributed
1. In Memory
2. Distributed
In case of in memory session, session are stored in memory of a server and if you are using two servers, other server will not have access to it as it is saved in memory of server 1. It is discouraged for load balanced applications.
While distributed sessions are stored in centralized place where both servers have access and it is considered to be more useful and reliable mechanism to store data in large applications. We will use it for this tutorial as we are also doing load balancing.
We are going to use SQL Server database to store session data and will implement Distributed SQL Server Cache.
Lets get started
project.json
Create a new file at root of project and name is project.json and copy below content inside it.
| { | |
| "tools": { | |
| "Microsoft.AspNetCore.Server.IISIntegration.Tools": { | |
| "version": "1.0.0-preview2-final", | |
| "imports": "portable-net45+win8+dnxcore50" | |
| }, | |
| "Microsoft.Extensions.Caching.SqlConfig.Tools": "1.0.0-preview2-final" | |
| }, | |
| "dependencies": {}, | |
| "frameworks": { | |
| "netcoreapp1.0": { | |
| "dependencies": { | |
| "Microsoft.NETCore.App": { | |
| "type": "platform", | |
| "version": "1.0.1" | |
| }, | |
| "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", | |
| "Microsoft.AspNetCore.Mvc": "1.0.0", | |
| "Microsoft.AspNetCore.Session": "1.0.0", | |
| "Microsoft.Extensions.Caching.SqlServer": "1.0.0", | |
| "Microsoft.Extensions.Configuration.Json": "1.0.0" | |
| }, | |
| "imports": "dnxcore50" | |
| } | |
| } | |
| } |
Run dotnet restore command in package manager console.
Create database and table to store session
Once SQLconfig tools are installed, now we need to create empty database in SQL server and after creation of database, we require table in our database in order to store session data. Following package manager console command can be used to create table and index.
dotnet sql-cache create "{Your connection string}" dbo SessionDataStore
You can find details of SQL server distributed cache here
You can find details of SQL server distributed cache here
Data Protection
Deployment of application to web farm requires Distributed caching and Data protection.
You need to add a data protection key file and need to keep same file in both servers in order to retrieve session data created by other server. Or you can keep protection key file in shared location from where both server can read.
You can find details of data protection here
You need to add a data protection key file and need to keep same file in both servers in order to retrieve session data created by other server. Or you can keep protection key file in shared location from where both server can read.
You can find details of data protection here
Startup.cs
Open startup.cs file and copy below code in ConfigureServices method.
Open startup.cs file and copy below code in ConfigureServices method.
| services.AddDataProtection() | |
| .PersistKeysToFileSystem(new DirectoryInfo("Path of protection file, typically shared drive")) | |
| .SetDefaultKeyLifetime(TimeSpan.FromDays(100)) | |
| .SetApplicationName("Your app name"); | |
| services.AddDistributedSqlServerCache(options => | |
| { | |
| options.ConnectionString = "SQL Server database connection string"; | |
| options.SchemaName = "dbo"; | |
| options.TableName = "SessionDataStore"; | |
| }); | |
| services.AddSession(options => { | |
| options.Cookie.Name = "Test.Session"; | |
| options.IdleTimeout = TimeSpan.FromMinutes(30); | |
| }); | |
| services.AddAuthentication("CookieAuthenticationScheme") | |
| .AddCookie("CookieAuthenticationScheme", options => | |
| { | |
| options.LoginPath = "/Account/Login"; | |
| options.ExpireTimeSpan = TimeSpan.FromMinutes(30); | |
| options.SlidingExpiration = true; | |
| }); services.AddMvc(); |
Add below code in Configure method
app.UseSession();
|
Save data to session
HttpContext.Session.SetString("StudentName","Lavister Todies");
Get data from session
var name = HttpContext.Session.GetString("StudentName");
All done !!!
Comments
Post a Comment