[英]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! 这是一个非常恼人的问题,我在我的asp.net核心项目上设置了cookie身份验证,有时会出现这种错误,有时却不会!
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 InvalidOperationException:没有配置身份验证处理程序来处理该方案:MyAuthScheme
This is really really annoying! 这真的很烦人! Here is my Startup.cs 这是我的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: Bruno的解决方案确实有效,但对我来说主要的问题是在UseIdentity之前有UseMVC,所以:
//Identity must be placed before UseMVC()
app.UseIdentity();
app.UseMvc();
I was facing this same issue and by digging into the source code, I discovered that for some reason, sometimes the IHttpAuthenticationFeature.Handler
doesn't get set, and that's the reason why this error occurs. 我正面临同样的问题,通过挖掘源代码,我发现由于某种原因,有时IHttpAuthenticationFeature.Handler
没有设置,这就是发生此错误的原因。 This is indeed very annoying and I fixed this issue by doing the following: 这确实非常烦人,我通过执行以下操作解决了这个问题:
Expose your CookieAuthenticationOptions
. 公开您的CookieAuthenticationOptions
。 I did this inside my Startup
class: 我在Startup
类中做了这个:
public static readonly CookieAuthenticationOptions cookieAuthenticationOptions = new CookieAuthenticationOptions
{
//... your settings here
};
Then in your Configure()
, it becomes: 然后在您的Configure()
,它变为:
app.UseCookieAuthentication(cookieAuthenticationOptions);
Then right before calling SignInAsync
, you do: 然后在调用SignInAsync
之前,您执行以下操作:
if (HttpContext.Features.Get<IHttpAuthenticationFeature>()?.Handler == null)
{
var handler = (AuthenticationHandler<CookieAuthenticationOptions>)Activator.CreateInstance(cookieAuthenticationHandlerType);
await handler.InitializeAsync(Startup.cookieAuthenticationOptions, HttpContext, logger, UrlEncoder.Default);
var feature = HttpContext.Features.Get<IHttpAuthenticationFeature>() ?? new HttpAuthenticationFeature();
feature.Handler = handler;
HttpContext.Features.Set(feature);
}
You also need: 你还需要:
private static readonly Type cookieAuthenticationHandlerType = typeof(CookieAuthenticationMiddleware).Assembly.GetType("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler");
And inject these 2 dependencies in your constructor like: 并在构造函数中注入这两个依赖项,如:
public LoginController(IHttpContextAccessor httpContextAccessor, ILoggerFactory loggerFactory)
{
this.HttpContext = httpContextAccessor.HttpContext;
this.logger = loggerFactory.CreateLogger(this.GetType());
}
Ps: I don't know if this is a bug or what... but I hope it gets fixed in the next release. Ps:我不知道这是一个bug还是什么......但我希望它能在下一个版本中修复。
I also ran into a similar issue using IdentityServer4 I needed to explicity add a signin-schema named the same as the variable or constant it was requesting. 我还遇到了类似的问题,使用IdentityServer4我需要明确添加一个与其请求的变量或常量相同的signin-schema。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
//Replace the Authentication Scheme with MyAuthScheme
AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
AutomaticAuthenticate = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.