没有配置身份验证处理程序来处理该方案

use*_*456 4 c# asp.net asp.net-core

这是一个非常恼人的问题,我在我的asp.net核心项目上设置了cookie身份验证,有时会出现这种错误,有时却不会!

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

例外是:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的身份验证码:

await HttpContext.Authentication.SignInAsync(Startup.AuthenticationSchemeName, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookie")));
Run Code Online (Sandbox Code Playgroud)

对此有何帮助?

fre*_*how 8

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

//Identity must be placed before UseMVC()
app.UseIdentity();
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)