[英].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.