简体   繁体   English

通过Windows Task Scheduler和HttpClient调用运行的.NET Core 2.1控制台应用程序导致401

[英].NET Core 2.1 console app running via Windows Task Scheduler with HttpClient calls result in 401

I've been running into an issue in upgrading an application from .NET Core 1.0.1 to .NET Core 2.1. 在将应用程序从.NET Core 1.0.1升级到.NET Core 2.1时遇到了一个问题。 This is a console app running via the Windows Task Scheduler that calls a set of web services hosted in IIS on the same server. 这是一个通过Windows Task Scheduler运行的控制台应用程序,该应用程序调用在同一服务器上的IIS中托管的一组Web服务。 After the upgrade, when attempting to run it on the host machine, it fails on the first API call, which returns a 401. IIS is set up to accept Anonymous Authentication and Windows Authentication, with the Negotiate and NTLM providers. 升级后,尝试在主机上运行它时,它在第一个API调用上失败,并返回401。IIS被设置为接受带有协商和NTLM提供程序的匿名身份验证和Windows身份验证。 The task scheduler sets the user that the console app should use. 任务计划程序设置控制台应用程序应使用的用户。 The code that makes the first API call looks like the following: 进行第一次API调用的代码如下所示:

using (var handler = new HttpClientHandler())
{
   handler.UseDefaultCredentials = true;

   var client = new HttpClient(handler);
   client.DefaultRequestHeaders.Accept.Clear();
   var result = await client.GetStringAsync(myUrlString);
   return Newtonsoft.Json.JsonConvert.DeserializeObject<List<MyResponseObject>>(result);
 }

I've also upgraded the API project to .NET Core 2.1, but I tried reverting back to the 1.0.1 version while running the console app as 2.1 (and conversely, running the API at 2.1 and the console app at 1.0.1 also works) to be sure that it's the console app with the problem. 我也已将API项目升级到.NET Core 2.1,但我尝试在以2.1版运行控制台应用程序的同时还原回1.0.1版(相反,也以2.1版运行API和以1.0.1版运行控制台应用程序起作用)以确保它是有问题的控制台应用程序。

The console app works fine when running from my localhost with the API hosted locally in IIS Express. 从我的本地主机运行,并且使用IIS Express本地托管的API时,控制台应用程序运行良好。

Any help would be much appreciated! 任何帮助将非常感激!

Try this. 尝试这个。 It work for our client app. 它适用于我们的客户应用程序。

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders
      .Accept
      .Add(new MediaTypeWithQualityHeaderValue("application/json"));

I've faced similar issue a few weeks ago, when moving from .NET Framework 4.6.x to .NET Core 2.1. 从.NET Framework 4.6.x迁移到.NET Core 2.1时,我在几周前也遇到过类似的问题。 The problem may be connected with new HttpHandler implemented in .NET Core 2.1. 该问题可能与.NET Core 2.1中实现的新HttpHandler有关。 There are various issues with this handler (for example, this ). 此处理程序存在各种问题(例如this )。 Even though this particular bug may not be completely related, the workaround described there may help: 即使此特定错误可能并不完全相关,但此处描述的解决方法可能会有所帮助:

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Windows 任务计划程序执行 .Net Core 2.0 控制台应用程序? - How to execute .Net Core 2.0 Console App using Windows Task Scheduler? ASP.NET Core 2.1 App在本地构建和运行成功,但无法通过Web Deploy发布到IIS - ASP.NET Core 2.1 App succeeds in Building and Running Locally but fails to Publish to IIS via Web Deploy 我们可以将 ASP.NET Core MVC Web 应用程序的一部分作为使用 Windows 任务调度程序调度的控制台应用程序运行吗 - Can we run part of an ASP.NET Core MVC web application as console application which is scheduled using Windows Task scheduler ASP.Net Core-添加计划任务 - ASP.Net Core - Adding Scheduler Task .NET Core 2.1 HttpClient不返回预期值 - .NET Core 2.1 HttpClient doesn't return expected values 在VS2015中通​​过控制台运行.NET Core App - Run .NET Core App via Console in VS2015 如何在启用Windows身份验证的情况下以编程方式启动和停止ASP.NET Core 2.1应用程序? - How to start and stop an ASP.NET Core 2.1 app programmatically with Windows Authentication enabled? 带有控制台应用程序的Windows Scheduler API与带有ASP.NET MVC的.NET调度程序工具相比,在我的ASP.NET MVC中执行长时间运行的进程 - Windows scheduler API with console application Vs .net scheduler tools with asp.net mvc to execute long running processes inside my asp.net MVC 如何在ASP.NET中使用Windows任务计划程序 - How to use Windows task Scheduler in ASP.NET .NET HttpClient 在非控制台应用程序中不会超时 - .NET HttpClient does not timed out for in non console app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM