C# 未配置任何身份验证处理程序来处理方案
这是一个非常烦人的问题,我正在我的asp.net核心项目上设置cookies身份验证,有时会发生此错误,有时不会 没有模式!它只是开始抛出错误,然后突然停止,然后重新开始 例外情况是: InvalidOperationException:未将身份验证处理程序配置为 处理方案:MyAuthScheme 这真的很烦人! 这是我的Startup.csC# 未配置任何身份验证处理程序来处理方案,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,这是一个非常烦人的问题,我正在我的asp.net核心项目上设置cookies身份验证,有时会发生此错误,有时不会 没有模式!它只是开始抛出错误,然后突然停止,然后重新开始 例外情况是: InvalidOperationException:未将身份验证处理程序配置为 处理方案:MyAuthScheme 这真的很烦人! 这是我的Startup.cs public class Startup { public const string AuthenticationSchemeName = "M
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();
}
}
这方面有什么帮助吗?我也面临着同样的问题,通过深入研究源代码,我发现由于某种原因,
IHttpAuthenticationFeature.Handler
有时无法设置,这就是发生此错误的原因。这确实很烦人,我通过以下操作解决了此问题:
公开您的CookieAuthenticationOptions
。我是在我的创业
课程中这样做的:
public static readonly CookieAuthenticationOptions cookieAuthenticationOptions = new CookieAuthenticationOptions
{
//... your settings here
};
然后在Configure()
中,它变成:
app.UseCookieAuthentication(cookieAuthenticationOptions);
然后,在调用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);
}
并在构造函数中注入这两个依赖项,如:
public LoginController(IHttpContextAccessor httpContextAccessor, ILoggerFactory loggerFactory)
{
this.HttpContext = httpContextAccessor.HttpContext;
this.logger = loggerFactory.CreateLogger(this.GetType());
}
Ps:我不知道这是一个bug还是什么。。。但我希望它在下一版本中得到修复。我也有同样的问题。Bruno的解决方案确实有效,但对我来说,主要问题是在UseIdentity之前有UseMVC,所以:
//Identity must be placed before UseMVC()
app.UseIdentity();
app.UseMvc();
我还遇到了一个类似的问题,使用IdentityServer4,我需要明确地添加一个名为与其请求的变量或常量相同的signin模式
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
//Replace the Authentication Scheme with MyAuthScheme
AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
AutomaticAuthenticate = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
您能简单地将常量AuthenticationSchemeName更改为字符串文字吗?所以:AuthenticationScheme=“CookieAuth”(在您的身份验证代码中也这样做)。告诉我它是否再次发生。是否有一个您使用的位置
MyAuthScheme
?Danny…抱歉,adem,我在更新AuthenticationSchemeName的值之前复制了错误消息。。。我编辑了这个问题以反映我当前的代码。我刚刚在aspnet/Identity github repo上提交了一个bug:你们能评论并添加相关的库版本吗,这可能有助于他们纠正这个问题。我也遇到了同样的问题,我已经尝试过你的代码,但不幸的是,它在我的情况下不起作用,因为HttpContext上有一个处理程序-我正在使用.useisintegration()
-它只是不处理我的自定义架构名称。文档让它看起来很简单……我只是偶然发现,我会在有时间的时候尝试实现它,重要的是尝试并理解为什么会发生这种错误,我得到了相同的错误,但原因似乎不同
//Identity must be placed before UseMVC()
app.UseIdentity();
app.UseMvc();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
//Replace the Authentication Scheme with MyAuthScheme
AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
AutomaticAuthenticate = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});