【问题标题】:No authentication handler is configured to handle the scheme没有配置身份验证处理程序来处理该方案
【发布时间】:2016-08-16 06:57:56
【问题描述】:

这是一个非常烦人的问题,我在我的asp.net核心项目上设置cookies认证,有时会出现这个错误,有时不会!

没有模式!它只是开始抛出错误并突然停止,然后重新开始......

例外是:

InvalidOperationException:未配置身份验证处理程序 处理方案:MyAuthScheme

这真的很烦人! 这是我的 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")));

有什么帮助吗?

【问题讨论】:

  • 您能简单地将常量 AuthenticationSchemeName 更改为字符串文字吗?所以: AuthenticationScheme = "CookieAuth" (并在您的身份验证代码中执行相同操作)。告诉我它是否再次发生。
  • 有你使用的地方MyAuthScheme吗?
  • 丹尼也一样...
  • 抱歉,我在更新 AuthenticationSchemeName 的值之前复制了错误消息...我编辑了问题以反映我当前的代码。

标签: c# asp.net asp.net-core


【解决方案1】:

我遇到了同样的问题。 Bruno 的解决方案确实有效,但对我来说主要问题是在 UseIdentity 之前有 UseMVC,所以:

//Identity must be placed before UseMVC()
app.UseIdentity();
app.UseMvc();

【讨论】:

    【解决方案2】:

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

    公开您的CookieAuthenticationOptions。我在我的Startup 班级里做了这个:

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

    你还需要:

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

    并在您的构造函数中注入这两个依赖项,例如:

    public LoginController(IHttpContextAccessor httpContextAccessor, ILoggerFactory loggerFactory)
    {
        this.HttpContext = httpContextAccessor.HttpContext;
        this.logger = loggerFactory.CreateLogger(this.GetType());
    }
    

    Ps:我不知道这是一个错误还是什么......但我希望它在下一个版本中得到修复。

    【讨论】:

    • 我刚刚在 aspnet/Identity github repo 上提交了一个错误:github.com/aspnet/Identity/issues/978 你们可以评论并添加相关的库版本,这可能有助于他们纠正问题。
    • 我遇到了同样的问题,我已经尝试过你的代码,但不幸的是它在我的情况下不起作用,因为 HttpContext 上有一个处理程序 - 我正在使用 .UseIISIntegration() - 它只是没有处理我的自定义架构名称。文档使它看起来如此简单docs.microsoft.com/en-us/aspnet/core/security/authentication/… ....
    • 我只是偶然发现了zogface.wordpress.com/2016/09/13/…,当我有时间的时候,我会尝试实现它,尝试理解为什么会发生这个错误很重要,我得到了同样的错误,但原因似乎与众不同
    【解决方案3】:

    我在使用 IdentityServer4 时也遇到了类似的问题,我需要明确添加一个与它请求的变量或常量同名的登录模式。

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        //Replace the Authentication Scheme with MyAuthScheme
        AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
        AutomaticAuthenticate = true,
        ExpireTimeSpan = TimeSpan.FromMinutes(60)
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多