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

时间:2017-08-22 12:47:02

标签: 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()

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

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

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

我发现了一些相关的Stack Overflow帖子(hereherehere),但它们都没有处理普通的Windows身份验证(答案似乎没有概括)。

3 个答案:

答案 0 :(得分:26)

在撰写帖子时,我记得遇到this subsection迁移指南。它说要添加

services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);

ConfigureServices函数。

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

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

修改

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

Microsoft.AspNetCore.Server.HttpSys.HttpSysDefaults.AuthenticationScheme

答案 1 :(得分:7)

Andreas的回答让我走上了正确的道路,但这对我有用:

添加了对Microsoft.AspNetCore.Authentication

的包引用

然后是 Startup.cs

using Microsoft.AspNetCore.Server.IISIntegration;

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    ...
}

答案 2 :(得分:0)

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