【发布时间】: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