I have enabled AAD Authentication for an Azure Function and then tried to consume the Function App (HTTP Trigger) in a web application but getting Unauthorized issue. I also tried consuming it by creating a function proxy but the issue still persists. Process Followed:
- Created two AD Application (Web App, Azure Functions) and gave the permission of Azure Functions AD to the Web App AD Created a basic http trigger function
- Enabled Authentication for Azure Functions by providing the details of Azure Functions
Created a web application and during the access token generation, provided the Client ID,Secret of web application and Audience URI( App ID) of Azure F Unctions AD.
ClientCredential clientCredential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientId"], ConfigurationManager.AppSettings["ida:SecretKey"]); AuthenticationContext authContext = new AuthenticationContext(Startup.Authority); AuthenticationResult result = await authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["azrfunc:ResourceID"], clientCredential); string requestUrl = "https://xxxx.azurewebsites.net/api/HttpTriggerCSharp1?code=Gxxxxx==&name=xxxx"; // Make the GET request HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); HttpResponseMessage response = client.SendAsync(request).Result;