gpt4 book ai didi

c# - ASP.NET Core 2.0 HttpSys Windows 身份验证因授权属性失败(InvalidOperationException : No authenticationScheme was specified)

转载 作者:IT王子 更新时间:2023-10-29 04:47:42 28 4
gpt4 key购买 nike

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

该应用程序相当简单,涉及以下内容:

  • 托管在 HttpSys(以前称为 WebListener)上
  • 使用 Windows 身份验证:options.Authentication.Schemes = AuthenticationSchemes.NTLM
  • 允许匿名认证:options.Authentication.AllowAnonymous = true(因为有些 Controller 不需要认证)
  • 需要身份验证的 Controller 用 [Authorize] 属性修饰。

该项目编译并启动得很好。它还为不需要身份验证的 Controller 的操作提供服务。

但是,只要我使用 [Authorize] 属性点击 Controller ,就会出现以下异常:

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 Core Web 应用程序(模型- View - Controller ) 轻松重现它。

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 。我还向 HomeController 类添加了 [Authorize] 属性。现在,它将产生与所示完全相同的异常。

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

最佳答案

在写这篇文章时,我记得遇到过 this subsection的迁移指南。它说要添加

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

ConfigureServices 函数。

考虑到常量的全名(尤其是 IISIntegration 让我失望),我最初认为这不适用于 HttpSys。此外,在撰写本文时,HttpSys documentation完全没有提到这一点。

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

编辑

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

Microsoft.AspNetCore.Server.HttpSys.HttpSysDefaults.AuthenticationScheme

关于c# - ASP.NET Core 2.0 HttpSys Windows 身份验证因授权属性失败(InvalidOperationException : No authenticationScheme was specified),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818095/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com