3

From my web service (A) usng impersonation i would like call a WebAPI service (B) using HttpClient. But the service B always gets the system user of service A even though i do impersonation there.

var baseUri = "http://service/api/"
var handler = new HttpClientHandler { UseDefaultCredentials = true };
var client = new HttpClient(handler) { BaseAddress = baseUri };
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("ContentType", new List<string> { "application/json"});

var dataDto = new DataDto();
var json = JsonConvert.SerializeObject(dataDto );
var content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var response = await client.PostAsync(SubUrl, content);

I know that Kerberos and SPN are set up correctly because it works using WebClient.

I think the problem is, that HttpClient.PostAsync creates a new task and therefore a new thread running under the credentials of the appPool of service A.

Does anyone know how i could force the task to run under the imperonated credentials?

I do not have access to the aspnet_config.config so the solution proveded here does not work for me.

Thanks a lot! Tschuege

Community
  • 1
  • 1
tschuege
  • 761
  • 1
  • 8
  • 20
  • var credentials = new NetworkCredential(userName, password); var handler = new HttpClientHandler { Credentials = credentials }; using (var http = new HttpClient(handler)) { // ... }? – codebased Sep 05 '14 at 12:59
  • @codebased, this is not impersonation. – Michael-O Sep 09 '14 at 20:27
  • are you using windows autho? – codebased Sep 09 '14 at 20:31
  • I finally had to do [this](http://stackoverflow.com/questions/10308938/unable-to-authenticate-to-asp-net-web-api-service-with-httpclient/10311823#10311823) anyway since i could not find any other solution – tschuege Nov 20 '14 at 09:28

0 Answers0