【问题标题】:ASP.NET Core 2.0 HttpSys Windows Authentication fails with Authorize attribute (InvalidOperationException: No authenticationScheme was specified)ASP.NET Core 2.0 HttpSys Windows 身份验证因 Authorize 属性而失败(InvalidOperationException:未指定身份验证方案)
【发布时间】:2018-01-30 18:24:52
【问题描述】:

我正在尝试将 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()

我开始摆弄项目模板,并注意到我可以使用标准模板 ASP.NET Core Web 应用程序(模型-视图-控制器) 轻松重现该模板和 Windows 身份验证。

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 身份验证(而且答案似乎没有笼统)。

【问题讨论】:

    标签: c# authentication asp.net-core


    【解决方案1】:

    在写这篇文章时,我记得遇到了迁移指南的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
    

    【讨论】:

    • 您可以将 IISIntegration.IISDefaults 替换为 HttpSys.HttpSysDefaults,它们恰好具有相同的值。
    • 谢谢!更新了答案。
    • 这个解决方案与@Tratcher 的建议一起为我工作。我的情况与OP的情况略有不同。我有一个全新的 Core 2.2 应用程序,它无需身份验证即可开始使用。激活 Windows 身份验证并将 [Authorize] 添加到控制器操作后出现“no authenticationScheme”错误。
    【解决方案2】:

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

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

    然后是 Startup.cs

    using Microsoft.AspNetCore.Server.IISIntegration;

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 谢谢!由于某种原因,这些设置似乎将自己“重置”回匿名。现在,我的源代码中附有一个指向此答案的链接。
      【解决方案4】:

      如果仍然无法正常工作,请确保您配置了 launchsettings.json

      {
        "iisSettings": {
          //...
          "windowsAuthentication": true,
          "anonymousAuthentication": true,
          //...
        },
        "profiles": {
          "IIS Express": {
         //...
          "windowsAuthentication": true,
          "anonymousAuthentication": true,
          //...
          }
        }
        //...more stuff
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-23
        • 1970-01-01
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 2018-06-23
        • 2018-03-30
        相关资源
        最近更新 更多