我有一个dotnet core 2.1 Web应用程序,其中包含一个React应用程序。使用IIS将应用程序部署到Windows服务器时,Windows身份验证可以正常工作。使用IIS从Visual Studio 2017调试应用程序时,身份验证可以正常工作。但是,从VS Code运行应用程序时,身份验证失败。事实证明,这非常困难,尽管GitHub和Microsoft文档页面上都有文档,但是该文档的实现未能解决问题。查看日志文件,我发现有一个System.Invalid操作异常,没有找到默认或质询方案。
错误: System.InvalidOperationException:未指定authenticationScheme,也没有找到DefaultChallengeScheme。 在Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext上下文,字符串方案,AuthenticationProperties属性) 在Microsoft.AspNetCore.Mvc.ChallengeResult.ExecuteResultAsync(ActionContext上下文) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult结果) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAlwaysRunResultFilters() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 在Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) 在Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext上下文)
LaunchSettings.json:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationName": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000/"
}
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
if (HostingEnvironment.IsDevelopment())
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.Configure<IISOptions>(options => {
options.AutomaticAuthentication = true;
options.ForwardClientCertificate = true;
});
}
else
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
services.AddMvc();
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ReactApp/build";
});
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
var logFactory = NLogBuilder.ConfigureNLog("nlog.config");
logFactory.KeepVariablesOnReload = true;
var logger = logFactory.GetCurrentClassLogger();
try
{
CreateWebHostBuilder(args).UseNLog().Build().Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception");
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
我还尝试使用预处理器指令,Visual Studio 2017也对此作出了响应。但是,这是Visual Studio Code的已知问题。然后,我尝试使用扩展方法来确定请求是否来自本地主机,然后根据环境默认设置该组。这也没有用。然后,我尝试区分本地开发环境和带NTLM based on this Stack Overflow question的HttpSys,但这还是行不通的。以上代码已从上述尝试和原始代码中还原。最后,我仍然尝试在Visual Studio Code中使用IIS Express和IISExpress执行程序扩展,但均无济于事。