I have a regular ASP .NET Web Application that I deployed into Azure using App Services. After I deployed it, I enabled the App Service Authentication and configured Azure Active Directory. That allowed me to publish my Web Application and also have authentication so only people who are part of the Active Directory can log in.
On the other hand, I deployed an ASP .NET Web API as a Cloud Service. The Web Application is supposed to load some information calling the Web API (which loads some data from a SQL Database and return it back) and then displaying the information back in the UI. However, when I call the API, the Azure Active Directory credentials doesn't get passed from the Web App to the Web API.
Below is some code that I have in the Web Application that calls the Web API endpoint.
public string GetStringAsync(string endPoint)
{
HttpClientHandler handler = new HttpClientHandler
{
UseDefaultCredentials = true
};
handler.PreAuthenticate = true;
using (HttpClient client = new HttpClient(handler))
{
return client.GetStringAsync(endPoint).Result;
}
}
But when I try to get the username when the call goes to the API, I get an empty string. Below is what I do to capture the Identity of who called the API:
HttpContext.Current.User.Identity.Name;
How can I get the Azure Active Directory domain\user information at the time the Web Application calls the Web API? Possibly my function to call the API is completely wrong?