.NET Core 2.1 Web API模拟会在处理错误时导致WSALookupServiceEnd

Bea*_*vic 5 c# impersonation .net-core asp.net-core-webapi

我正在尝试在.NET Core 2.1 Web-API中进行模拟。因此,此Web-API使用HttpClient调用了另一个Web-API,我需要调用第一个的用户也必须是正在执行第二个的用户。同样的情况也可以在通过此调用运行完整框架的另一个Web-API中使用:

((WindowsIdentity)_httpContextAccessor.HttpContext.User.Identity).Impersonate()
Run Code Online (Sandbox Code Playgroud)

由于Impersonate().NET Core 2.1中没有此功能,因此我使用搜索了一些示例,WindowsIdentity.RunImpersonated并尝试了与此类似的不同版本的代码:

WindowsIdentity identity = (WindowsIdentity)m_contextAccessor.HttpContext.User.Identity;
HttpClient client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true });

await WindowsIdentity.RunImpersonated(identity.AccessToken, async () =>
{
    var request = new HttpRequestMessage(HttpMethod.Get, url);
    var response = await client.SendAsync(request);
});
Run Code Online (Sandbox Code Playgroud)

这将引发错误,client.SendAsync错误消息是这样的:

在此调用仍在处理期间,已对WSALookupServiceEnd进行了调用。呼叫已被取消---> System.Net.Http.HttpRequestException

堆栈跟踪的开始:

在System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32端口,CancellationToken cancelleToken)-内部异常堆栈跟踪的结尾-在System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32端口,CancellationToken System.Threading.Tasks.ValueTask`1.get_Result()处的System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage请求,CancellationToken cancelleToken)

是否有其他人看到此错误或对如何解决此问题有任何见解?我尝试了不同版本的代码来RunImpersonatedHttpContext用户调用,并且都导致了相同的错误。

感谢您的输入

Chr*_*les 0

尽管这个错误非常模糊,但我很确定这个问题与服务器拒绝连接到端点有关,或者它找不到端点。我的问题是网络服务器看不到端点 - 仅供参考,不要使用“localhost:xxxx”指向本地端口。相反,请在配置中使用服务器完整 IP 地址。localhost 并不总是在本地 DNS 中解析。

TLDR:确保您的 Web 服务器可以通过授权 ping 端点服务器和端口。