- First, we add the following library to the project:
"Microsoft.AspNetCore.Session": "2.0.0"
- Next, let's configure Session in Startup.cs. We also add the MVC services to use the ASP.NET MVC pipeline:
public void ConfigureServices(IServiceCollection services) { services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); options.CookieName = ".Session"; }); services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
- Next, we create some helper methods to serialize objects, to get and store objects in Session:
// Convert an object to a byte array public ...