I have two sites, A
and B
. A
consumes an API that B
exposes, and B
requires Windows authentication. Both sites live in Domain D
.
The API is consumed via HttpClient
, and when site A
is run locally, under my domain account (which is in Domain P
), access is granted. In this case, HttpClient
is instantiated like so:
using(var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials: true }))
When A
is deployed to a testing server, the above results in a 401 Unauthorized
response. The application pool on the testing server is running under a service account in domain D
.
When explicitly using that service account like this:
var credential = new NetworkCredential("service-account", "password", "D");
var cache = new CredentialCache
{
{
new Uri(apiServerUri), "NTLM", credential
}
};
var handler = new HttpClientHandler
{
Credentials = cache
};
using(var client = new HttpClient(handler))
...
And again running site A
locally, access is still granted. Access is also granted when accessing the API directly via browser, and specifying the service account credentials. Logs indicate that it is definitely the service account being used to access the API.
Deploying the above back to the testing server still results in 401 Unauthorized
.
Deploying site A
to a local instance of IIS, also successfully consumes the API of B
.
Running site B
locally, and then accessing it via site A
locally, results in a 401 Unauthorized
.
Accessing the API through a browser on the testing server where A
is deployed, and specifying the service account credentials, also gives a 401 Unauthorized
.
I'm not sure where to go from here - am I missing something in the code to get this working? Or is it likely to be an IIS or AD issue?