I'm trying to do impersonation in a .NET Core 2.1 Web-API. So this Web-API calls another Web-API using HttpClient and I need the user that called the first one to also be the one who is executing the second one. The same scenario does work from another Web-API running with the full framework with this call:
((WindowsIdentity)_httpContextAccessor.HttpContext.User.Identity).Impersonate()
Since Impersonate()
is not available in .NET Core 2.1 I searched for some samples with WindowsIdentity.RunImpersonated
and tried different versions of code similar to this:
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);
});
This throws an error at client.SendAsync
and the error message is this:
A call to WSALookupServiceEnd was made while this call was still processing. The call has been canceled ---> System.Net.Http.HttpRequestException
Start of the Stack Trace:
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask`1.get_Result() at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Has anyone else seen this error or has any insight on how to solve this? I tried different versions of code for calling RunImpersonated
with the HttpContext
user and all lead to the same error.
Thanks for any input