Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
I recently needed to create a client app that used an AAD application to authenticate with an Azure Function that was configured with the AAD Easy Auth flow. Documenting it here seemed like it might add value to the interwebz.
Once it is created, create a simple function that uses an HTTP trigger:
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
return (ActionResult)new OkObjectResult("Hello world");
}
Use a function like this to access your Azure Function App
static async Task Post()
{
string aadInstance = "https://login.windows.net/{0}";
string tenant = "yourtenant";
string serviceResourceId = "AppIdUrl from your Azure Function AAD app goes here";
string clientId = "app id of your client AAD app";
string appKey = "app key of your client AAD app";// Get auth token and add the access token to the authorization header of the request.
var httpClient = new HttpClient();
var authContext = new AuthenticationContext(string.Format(CultureInfo.InvariantCulture, aadInstance, tenant));
var clientCredential = new ClientCredential(clientId, appKey);
AuthenticationResult result = await authContext.AcquireTokenAsync(serviceResourceId, clientCredential);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);// Send the request and read the response
HttpResponseMessage response = await httpClient.GetAsync("The function url that you saved from earlier");
Console.WriteLine($"{(int)(response.StatusCode)} {response.ReasonPhrase}");
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Will this work for you? I hope so! But I'm far from an expert. Hopefully it helps someone or at least points you in the right direction.
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in