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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With