'Why is await HttpContext.SignInAsync("MyCookieAuth", claimsPrincipal); throwing No sign-in authentication handlers are registered? (Razor Page model)

I'm trying to do a simple user authentication with the following code

public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid) return Page();
            
            //verfiy the credential
            if(Credential.UserName == "admin" && Credential.Password == "password")
            {
                //Creating the security context
                var claims = new List<Claim> {
                    new Claim(ClaimTypes.Name, "admin"),
                    new Claim(ClaimTypes.Email, "[email protected]")
                };

                var identity = new ClaimsIdentity(claims, "MyCookieAuth");
                ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);

                
                await HttpContext.SignInAsync("MyCookieAuth", claimsPrincipal);
                
                return RedirectToPage("/Index");
            }
            return Page();
        }

await HttpContext.SignInAsync throws:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception occurred while processing the request. InvalidOperationException: No sign-in authentication handlers are registered. Did you forget to call AddAuthentication().AddCookie("MyCookieAuth",...)? . . . at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source