【问题标题】:.NET Core 2.1 Web API Impersonation causes WSALookupServiceEnd while processing error.NET Core 2.1 Web API 模拟导致 WSALookupServiceEnd 处理错误
【发布时间】:2019-01-07 10:03:41
【问题描述】:

我正在尝试在 .NET Core 2.1 Web-API 中进行模拟。所以这个 Web-API 使用 HttpClient 调用另一个 Web-API,我需要调用第一个的用户也是执行第二个的用户。 使用此调用运行完整框架的另一个 Web-API 确实可以实现相同的场景:

((WindowsIdentity)_httpContextAccessor.HttpContext.User.Identity).Impersonate()

由于 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);
});

这会在client.SendAsync 处引发错误,错误消息如下:

在调用 WSALookupServiceEnd 时进行了调用 加工。通话已取消---> System.Net.Http.HttpRequestException

堆栈跟踪的开始:

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

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

感谢您的任何意见

【问题讨论】:

  • 你有想过这个吗?如果是这样,请留下答案:)

标签: c# .net-core asp.net-core-webapi impersonation


【解决方案1】:

从 .NET Core 2.1 开始,SocketsHttpHandler 类提供更高级别的 HTTP 网络类(如 HttpClient)使用的实现。 试试disable这个功能,看看异常是否消失。

【讨论】:

  • 这可能很好地解决问题,因为“模拟用户没有权限在本地机器上打开套接字。这就是应用程序抛出异常的原因。”使用以下命令关闭:AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
【解决方案2】:

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

TLDR:确保您的网络服务器可以 ping 端点服务器和具有权限的端口。

【讨论】:

    【解决方案3】:
    await WindowsIdentity.RunImpersonated(identity.AccessToken, async () =>
    {
        //this should give you the impersonated user
        var impersonatedUser = WindowsIdentity.GetCurrent().Name;
    
        var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true });
        var request = new HttpRequestMessage(HttpMethod.Get, url);
        var response = await client.SendAsync(request);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2019-05-23
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多