How to do it...

We'll create a new ASP.NET Core Web project and add the required libraries to use cookies to hold authenticated user information in it:

  1. Let's create a new ASP.NET Core web project and configure it using cookie authentication:
dotnet new web -n CookieAuthenticationSample 
dotnet add package Microsoft.AspNetCore.Authentication.Cookies 
dotnet restore 
  1. Now we need to add the following line in the Startup.cs file before the app.UseMvc() or app.UseMvcWithDefaultRoute() lines in the Configure() method:
 app.UseAuthentication(); 
  1. We also need to add the following line in ConfigureServices() method, before the services.AddMvc() line, as follows:
 services.AddAuthentication("CookieAuthenticationScheme") .AddCookie("CookieAuthenticationScheme", ...

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.