ASP.NET Core 2.0 HttpSys Windows身份验证失败并带有Authorize属性(InvalidOperationException:未指定authenticationScheme)

And*_*eas 18 c# authentication asp.net-core

我正在尝试将ASP.NET Core 1.1应用程序迁移到ASP.NET Core 2.0.

该应用程序非常简单,涉及以下内容:

  • 主持HttpSys(原WebListener)
  • 使用Windows身份验证: options.Authentication.Schemes = AuthenticationSchemes.NTLM
  • 允许匿名身份验证:( options.Authentication.AllowAnonymous = true因为有些控制器不需要身份验证)
  • 需要身份验证的控制器使用该[Authorize]属性进行修饰.

该项目编译并启动就好了.它还提供不需要身份验证的控制器的操作.

但是,只要我使用该[Authorize]属性命中控制器,就会出现以下异常:

System.InvalidOperationException: No authenticationScheme was specified,
and there was no DefaultChallengeScheme found.
   at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.ChallengeResult.<ExecuteResultAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()
Run Code Online (Sandbox Code Playgroud)

我开始摆弄项目模板,并注意到我可以使用带有Windows身份验证的标准模板ASP.NET Core Web Application(模型 - 视图 - 控制器)轻松地重现这一点.

Program.cs文件更改如下:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseHttpSys(options =>
            {
                options.Authentication.Schemes = AuthenticationSchemes.NTLM;
                options.Authentication.AllowAnonymous = true;
                options.MaxConnections = 100;
                options.MaxRequestBodySize = 30000000;
                options.UrlPrefixes.Add("http://localhost:5000");
            })
            .UseStartup<Startup>()
            .Build();
Run Code Online (Sandbox Code Playgroud)

这直接来自HttpSys文档.我还将该[Authorize]属性添加到了HomeController类中.现在,它将产生与所示完全相同的异常.

我发现了一些相关的Stack Overflow帖子(这里,这里这里),但没有一个处理普通的Windows身份验证(答案似乎没有概括).

And*_*eas 27

在写这篇文章的时候,我记得遇到过迁移指南的这一小节.它说要添加

services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);
Run Code Online (Sandbox Code Playgroud)

ConfigureServices功能.

我最初认为这不适用于HttpSys,因为常量的全名(特别是IISIntegration把我扔了).此外,在撰写本文时,HttpSys文档完全没有提到这一点.

对于那些以完整.NET Framework为目标的人,这需要安装Microsoft.AspNetCore.AuthenticationNuGet包.

编辑

正如Tratcher指出的那样,HttpSys你应该使用的命名空间中有一个类似的常量:

Microsoft.AspNetCore.Server.HttpSys.HttpSysDefaults.AuthenticationScheme
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用HttpSys.HttpSysDefaults替换IISIntegration.IISDefaults,它们碰巧具有相同的值. (4认同)

fia*_*iat 7

安德烈亚斯的回答让我走上了正确的道路,但这对我有用:

添加了包的引用 Microsoft.AspNetCore.Authentication

然后是Startup.cs

using Microsoft.AspNetCore.Server.IISIntegration;

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    ...
}
Run Code Online (Sandbox Code Playgroud)


小智 6

另一件事,如果您已经添加了 services.AddAuthentication(IISDefaults.AuthenticationScheme); 确保在应用程序-> 身份验证下的 iis 中打开身份验证类型(窗口、表单)。我的都被禁用了,即使代码到位也会出现这个错误。