没有配置身份验证处理程序来处理方案 [英] No authentication handler is configured to handle the scheme

查看:71
本文介绍了没有配置身份验证处理程序来处理方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常烦人的问题,我正在asp.net核心项目上设置Cookie身份验证,有时会发生此错误,有时却不会!

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...

例外是:

InvalidOperationException:没有身份验证处理程序配置为处理方案:MyAuthScheme

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

这真的很烦人!这是我的Startup.cs

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

这是我的验证码:

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

对此有任何帮助吗?

推荐答案

我也遇到了同样的问题,通过深入研究源代码,我发现由于某种原因,有时 IHttpAuthenticationFeature.Handler 没有设置,这就是发生此错误的原因.这确实很烦人,我通过执行以下操作解决了这个问题:

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. This is indeed very annoying and I fixed this issue by doing the following:

公开您的 CookieAuthenticationOptions .我是在 Startup 类中完成此操作的:

Expose your CookieAuthenticationOptions. I did this inside my Startup class:

public static readonly CookieAuthenticationOptions cookieAuthenticationOptions = new CookieAuthenticationOptions
{
    //... your settings here
};

然后在您的 Configure()中,它变为:

Then in your Configure(), it becomes:

app.UseCookieAuthentication(cookieAuthenticationOptions);

然后在调用 SignInAsync 之前,执行以下操作:

Then right before calling SignInAsync, you do:

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

您还需要:

private static readonly Type cookieAuthenticationHandlerType = typeof(CookieAuthenticationMiddleware).Assembly.GetType("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler");

然后将这2个依赖项注入您的构造函数中,如:

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:我不知道这是一个错误还是什么...但是我希望它在下一个版本中得到解决.

Ps: I don't know if this is a bug or what... but I hope it gets fixed in the next release.

这篇关于没有配置身份验证处理程序来处理方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆