我正在尝试将 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 应用程序(模型-视图-控制器(的标准模板轻松重现它。
程序.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 文档。 此外,我将[Authorize]
属性添加到HomeController
类中。 现在,它将产生与所示完全相同的异常。
我找到了一些相关的堆栈溢出帖子(这里,这里和这里(,但没有一个涉及普通的Windows身份验证(答案似乎并不普遍(。
在撰写这篇文章时,我记得遇到了迁移指南的这一小节。 它说要添加
services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);
到ConfigureServices
功能。
我最初认为这不适用于 HttpSys,因为常量的全名(尤其是IISIntegration
让我失望(。 此外,在撰写本文时,HttpSys 文档完全没有提及这一点。
对于那些面向完整.NET Framework的用户,这需要安装Microsoft.AspNetCore.Authentication
NuGet 包。
编辑
正如 Tratcher 指出的那样,您应该使用HttpSys
命名空间中有一个类似的常量:
Microsoft.AspNetCore.Server.HttpSys.HttpSysDefaults.AuthenticationScheme
安德烈亚斯的回答让我走上了正确的道路,但这就是对我有用的:
添加了对Microsoft.AspNetCore.Authentication
的包引用
然后是启动.cs
using Microsoft.AspNetCore.Server.IISIntegration;
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication(IISDefaults.AuthenticationScheme);
...
}
另一件事,如果你已经添加了services.AddAuthentication(IISDefaults.AuthenticationScheme);
确保在 IIS 中的"应用程序→身份验证"下打开身份验证类型(窗口、窗体(。
我的都被禁用了,即使代码到位,也会出现此错误。
如果它仍然不起作用,请确保您配置了 launchsettings.json
{
"iisSettings": {
//...
"windowsAuthentication": true,
"anonymousAuthentication": true,
//...
},
"profiles": {
"IIS Express": {
//...
"windowsAuthentication": true,
"anonymousAuthentication": true,
//...
}
}
//...more stuff
}