No authentication handler is configured to handle the scheme
<p>This is a very annoying problem, I'm setting up cookies authentication on my asp.net core project and sometimes this error occurs and sometimes it doesn't!</p>

<p>There is no pattern! It just starts throwing the error and suddenly it stops, then start again...</p>

<p>The exception is: </p>

<blockquote>
  <p>InvalidOperationException: No authentication handler is configured to
  handle the scheme: MyAuthScheme</p>
</blockquote>

<p>This is really really annoying!
Here is my Startup.cs</p>

<pre><code>public class Startup
{
    public const string AuthenticationSchemeName = "MyAuthScheme";

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();

        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationScheme = AuthenticationSchemeName,
            LoginPath = new PathString("/login"),
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            SlidingExpiration = true,
        });

        app.UseStaticFiles();
        app.UseMvc();
    }
}
</code></pre>

<p>And this is my authentication code:</p>

<pre><code>await HttpContext.Authentication.SignInAsync(Startup.AuthenticationSchemeName, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookie")));
</code></pre>

<p>any help on this?</p>