title: Cookie authentication is not working in ASP.NET Core application
body: <p>I am trying to develop a project in .NET Core 3.1. I am trying to implement cookie based authentication in my project. My login function is:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; Login(UserLoginModel userModel)
  {
      if (!ModelState.IsValid)
      {
          return View(userModel);
      }
  
      if (userModel.Email == "admin@test.com" &amp;&amp; userModel.Password == "123")
      {
          var identity = new ClaimsIdentity(IdentityConstants.ApplicationScheme);
          identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "User Id"));
          identity.AddClaim(new Claim(ClaimTypes.Name, "User Name"));
  
          var principal = new ClaimsPrincipal(identity);
  
          await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, principal);
  
          return RedirectToAction(nameof(HomeController.Index), "Home");
      }
      else
      {
          ModelState.AddModelError("", "Invalid UserName or Password");
          return View();
      }
  }
  </code></pre>
  
  <p>To implement cookie based authentication, I put the below code in my ConfigureService method of Startup class:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddControllersWithViews();
  
      services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
      {
          // This lambda determines whether user consent for non-essential cookies is needed for a given request.  
          options.CheckConsentNeeded = context =&gt; true;
          options.MinimumSameSitePolicy = SameSiteMode.None;
      });
  
      services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
          .AddCookie(options =&gt;
          {
              options.Cookie.Name = "_auth";
              options.Cookie.HttpOnly = true;
              options.LoginPath = new PathString("/account/login");
              options.LogoutPath = new PathString("/account/logout");
              options.AccessDeniedPath = new PathString("/account/login");
              options.ExpireTimeSpan = TimeSpan.FromDays(1);
              options.SlidingExpiration = false;
          });
  
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
  }
  </code></pre>
  
  <p>And the configure method of Startup class is:</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      app.UseStaticFiles();
      app.UseRouting();
  
      app.UseAuthentication();
      app.UseAuthorization();
  
      app.UseEndpoints(endpoints =&gt;
      {
          endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller=Home}/{action=Index}/{id?}");
      });
  }
  </code></pre>
  
  <p>But the problem is each time I try to login, the below exception occur in below code of login action method.</p>
  
  <blockquote>
    <p>await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme,
    principal)</p>
  </blockquote>
  
  <p>The exception occured is given below:</p>
  
  <blockquote>
    <p>InvalidOperationException: No sign-in authentication handler is
    registered for the scheme 'Identity.Application'. The registered
    sign-in schemes are: Cookies. Did you forget to call
    AddAuthentication().AddCookies("Identity.Application",...)?
    Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext
    context, string scheme, ClaimsPrincipal principal,
    AuthenticationProperties properties)
    _01_AuthenticationDemo.Controllers.AccountController.Login(UserLoginModel userModel) in AccountController.cs
    +
                    await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme,
    principal);</p>
  </blockquote>
  
  <p>Can anyone give me suggestion to solve the problem.</p>