2
votes

Currently, We are hosting a .net core 3.1 webapp in IIS (DEV server), and it is using Windows Authentication. We want to impersonated caller's credential when we call another webapi in one of the controller action, here is our code

        [Authorize]
        public IActionResult Get()
        {
            _logger.LogInformation("Boom! In Get");

            WindowsIdentity.RunImpersonated(((WindowsIdentity)HttpContext.User.Identity).AccessToken, () =>
            {
                _logger.LogInformation($"Received request");

                HttpClient _client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true })
                {
                    BaseAddress = new Uri("http://SomeTestUrl.com/")
                };

                var result = _client.GetAsync("/api/program").Result;
                var json = result.Content.ReadAsStringAsync().Result;


                _logger.LogInformation($"Returning response");
            });

            return Ok("True");
        }

It runs fine on my local machine with IISExpress but its failing on DEV server. I am getting below error.

Any idea why am I getting this error? What is the correct way of impersonating user in controller action?

Other observations:

System.Net.Http.HttpRequestException: This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. ---> System.Net.Sockets.SocketException (11002): This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. 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.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

1

1 Answers

2
votes

We have been experiencing exactly the same issue and had to resort to using the loopback ip address to avoid requiring DNS resolution (http://127.0.0.1/MyAddress). Many hours have been poured into trying to resolve this issue without any joy and I can only assume this is a bug in .net core. I hope this helps.