I am developing an API using C# ASP.Net Core runtime and I'm planning to host this in IIS.
One of the requirement is to get information from another RESTful API server which currently has windows authentication (LDAP authentication).
I already have a solution by using a service account which is hardcoding the service account name and password in appsettings.json.
The code is as follows
var serviceApi = _config["Api:ServiceApi"].ToString(); // this is the URL of other RESTful API from appsettings.json
var serviceAccount = _config["ServiceAccount:UserName"].ToString(); // service account username from appsettings.json
var serviceAccountPwd = _config["ServiceAccount:Password"].ToString(); // service account password which is currently shown as clear text in appsettings.json
var serviceAccountDomain = _config["ServiceAccount:Domain"].ToString(); // service account domain name from appsettings.json
var client = new WebClient();
CredentialCache cc = new CredentialCache();
cc.Add(
new Uri(serviceApi),
"NTLM",
new NetworkCredential(serviceAccount, serviceAccountPwd, serviceAccountDomain));
client.Credentials = cc;
client.Headers["Content-Type"] = "application/json";
string response = client.DownloadString($"{serviceApi}/User");
The downside of this solution is, the password of service account is visible in appsettings.json. I need other solution which will not expose the password in clear text.
One of the solution that I can think of is to store service account information in IIS application pool identity. However, I still cannot make WebClient to use IIS app pool identity. What I can do so far to make WebClient to use the NTLM credential of my own from windows.
Here's the code
var serviceApi = _config["Api:ServiceApi"].ToString();
var client = new WebClient();
client.UseDefaultCredentials = true; // this is not getting credential from IIS app pool identity but rather from logged in account in windows operating system
client.Headers["Content-Type"] = "application/json";
string response = client.DownloadString($"{serviceApi}/User");
Could please help me with how to use IIS app pool identity as credential for other RESTful API application?
Ultimately we found we could not achieve this, as technically it compromises the security of the logged-in user unless you enable windows/NTLM auth on the API server (and this requires the browser/client to actively enable sharing the user's windows credentials with the server).
The solution we ended up using was using a pre-shared secret, in the form of an AppId and AppSecret, which seems to be the standard approach. Essentially we issued the calling application a username and password which exists outside of AD.
Seems you have some misunderstanding about Windows Authentication.
So, given all the above. You want to use Windows Integrated Security with Kerberos SPNEGO between your frontend webserver and the backend webserver.
The following will be a short troubleshooting guide.