How to do it......

  1. First, we add the following library to the project:
 
      "Microsoft.AspNetCore.Session": "2.0.0" 
 
  1. 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?}"); 
            }); 
        } 
  1. Next, we create some helper methods to serialize objects, to get and store objects in Session:
 // Convert an object to a byte array public ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.