Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No authentication handler is configured to handle the scheme

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!

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

The exception is:

InvalidOperationException: No authentication handler is configured to handle the scheme: MyAuthScheme

This is really really annoying! Here is my Startup.cs

public class Startup
{
    public const string AuthenticationSchemeName = "MyAuthScheme";

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        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();
    }
}

And this is my authentication code:

await HttpContext.Authentication.SignInAsync(Startup.AuthenticationSchemeName, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookie")));

any help on this?

like image 851
user3900456 Avatar asked Dec 24 '22 02:12

user3900456


1 Answers

I had the same problem. Bruno's solution did work, but the main issue for me was that had UseMVC before UseIdentity, so:

//Identity must be placed before UseMVC()
app.UseIdentity();
app.UseMvc();
like image 180
freakshow Avatar answered Feb 16 '23 02:02

freakshow